Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Running lpr on windows from python

595 views
Skip to first unread message

loial

unread,
Apr 20, 2016, 9:57:36 AM4/20/16
to
I am trying to run lpr from python 2.7.10 on windows

However I always get the error
'C:/windows/system32/lpr.exe ' is not recognized as an internal or external command,
operable program or batch file.

Even though typing the same at the command prompt works OK


Any ideas?

I am using subprocess as follows

process = subprocess.Popen(commandline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

where command line is
C:/windows/system32/lpr.exe -S 172.28.84.38 -P RAW C:/john/myfile

Chris Angelico

unread,
Apr 20, 2016, 10:07:53 AM4/20/16
to
You''re running that through the shell, which means you have to abide
by shell rules. I don't have a Windows handy, but I'm pretty sure its
shell isn't happy with forward slashes in the command line; I might be
wrong there.

My recommendation: Split that into separate arguments, pass them as a
list, and remove shell=True. And unless you need to be completely
explicit for some reason (eg to protect against path-based exploits),
cut the first argument to just "lpr" and let the binary be found
anywhere.

ChrisA

Tim Golden

unread,
Apr 20, 2016, 10:09:11 AM4/20/16
to
Ummm.. Do you actually have a program called lpr.exe in that location?
It's not usual on Windows. (I rather assume you do since you give the
full path, but still...)

IOW, what happens if you type:

dir C:\windows\system32\lpr.exe

at a command promopt?

Also: are you on a 64-bit system? If so, c:\windows\system32 probably
isn't where you think it is. cf, for example:

https://mail.python.org/pipermail/python-win32/2012-March/012121.html

TJG

Random832

unread,
Apr 20, 2016, 10:09:30 AM4/20/16
to
On Wed, Apr 20, 2016, at 09:57, loial wrote:
> I am trying to run lpr from python 2.7.10 on windows
>
> However I always get the error
> 'C:/windows/system32/lpr.exe ' is not recognized as an internal or
> external command,
> operable program or batch file.
>
> Even though typing the same at the command prompt works OK

It does? This command doesn't exist on my machine. It's not a standard
part of windows.

Just to check though, are you 64-bit windows, and 32 or 64 bit python?
(To find out what kind of windows, go to the system control panel - to
find out if python is 64-bit look at the value of sys.maxsize, it's
2147483647 on 32-bit systems and 9223372036854775807 on 64-bit)

> Any ideas?
>
> I am using subprocess as follows
>
> process = subprocess.Popen(commandline, shell=True,
> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>
> where command line is
> C:/windows/system32/lpr.exe -S 172.28.84.38 -P RAW C:/john/myfile

If all you want to do is print a text file, see
http://www.robvanderwoude.com/printfiles.php - these commands may not
let you do whatever you're trying to do with that IP address though.

loial

unread,
Apr 20, 2016, 10:21:29 AM4/20/16
to
As I said, the lpr command works fine from the command prompt but not from python.

Everything is 64-bit (windows server 2012).

loial

unread,
Apr 20, 2016, 10:25:57 AM4/20/16
to
I get the same issue if I just specify "lpr" rather than a full path, i.e. it works from the command prompt(with forward slashes), but not from python

Tim Golden

unread,
Apr 20, 2016, 10:58:57 AM4/20/16
to
On 20/04/2016 15:21, loial wrote:
> As I said, the lpr command works fine from the command prompt but not from python.

Sorry; I did miss that.

> Everything is 64-bit (windows server 2012).
>

Is the Python installation also 64-bit?

c:\python27\python.exe -c "import platform; print platform.architecture()"

If it is, then I'm not sure what's going on.

If it's not, then try copying the lpr.exe to c:\windows\syswow64 and try
again. (Or to some other place to which you have access).

TJG

Stephen Hansen

unread,
Apr 20, 2016, 5:02:18 PM4/20/16
to
On Wed, Apr 20, 2016, at 06:57 AM, loial wrote:
> process = subprocess.Popen(commandline, shell=True,
> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>
> where command line is
> C:/windows/system32/lpr.exe -S 172.28.84.38 -P RAW C:/john/myfile

Try making command line:
commandline = r"C:\windows\system32\lpr.exe -S 172.28.84.38 -P RAW
C:\john\myfile"

The r in front of the string makes it a raw string so you don't have to
double up the slashes.

---
Stephen Hansen
m e @ i x o k a i . i o

eryk sun

unread,
Apr 20, 2016, 11:11:48 PM4/20/16
to
On Wed, Apr 20, 2016 at 9:58 AM, Tim Golden <ma...@timgolden.me.uk> wrote:
> If it's not, then try copying the lpr.exe to c:\windows\syswow64 and try
> again. (Or to some other place to which you have access).

WOW64 in Windows 7+ has a virtual "SysNative" directory that accesses
the native 64-bit system directory:

if '32bit' in platform.architecture():
lpr = os.path.join(os.environ['SystemRoot'], 'SysNative', 'lpr.exe')
else:
lpr = os.path.join(os.environ['SystemRoot'], 'System32', 'lpr.exe')

loial

unread,
Apr 22, 2016, 7:30:38 AM4/22/16
to
Nothing seems to work.

Even doing

import os
os.system("lpr")

still returns
'lpr' is not recognized as an internal or external command,operable program or batch file.


Even though I can run lpr fine from the command prompt

loial

unread,
Apr 22, 2016, 11:08:04 AM4/22/16
to
I finally found the solution here :

http://www.tomshardware.co.uk/forum/240019-44-error-windows

Copied lpr.exe, lprhelp.dll, and lprmonui.dll from the System32 folder to the sysWOW64 folder

Thanks for all your efforts

eryk sun

unread,
Apr 22, 2016, 1:05:28 PM4/22/16
to
On Fri, Apr 22, 2016 at 10:07 AM, loial <jldun...@gmail.com> wrote:
> I finally found the solution here :
>
> http://www.tomshardware.co.uk/forum/240019-44-error-windows
>
> Copied lpr.exe, lprhelp.dll, and lprmonui.dll from the System32 folder to the sysWOW64 folder

Using the virtual "SysNative" directory should work on Windows 7+
(Server 2008 R2). All you need is for CreateProcess to find the
executable. Finding the DLLs is done during process initialization, so
there's no need to copy them from the native System32 directory.

It works for me in 32-bit Python running in Windows 10:

>>> if '32bit' in platform.architecture():
... lpr = os.path.join(os.environ['SystemRoot'], 'SysNative', 'lpr.exe')
... else:
... lpr = os.path.join(os.environ['SystemRoot'], 'System32', 'lpr.exe')
...
>>> print(lpr)
C:\Windows\SysNative\lpr.exe
>>> subprocess.call(lpr)

Sends a print job to a network printer

Usage: lpr -S server -P printer [-C class] [-J job] [-o option]
[-x] [-d] filename

Options:
-S server Name or ipaddress of the host providing lpd service
-P printer Name of the print queue
-C class Job classification for use on the burst page
-J job Job name to print on the burst page
-o option Indicates type of the file (by default assumes
a text file)
Use &quot;-o l&quot; for binary (e.g. postscript) files
-x Compatibility with SunOS 4.1.x and prior
-d Send data file first1
0 new messages