stdin and py2exe

M

Mike Tammerman

Hi,

I want create a subprocess using Popen and pipe some input to it.
Although everything works perfectly while executing python in, it
doesn't work if I try with executables made by py2exe.

I think, stdin is invalidated if the program becomes an executable.
Because I get a "Bad file descriptor" exception in subprogram.py. I
will be more than apreciated, if any suggestions occur.

Thanks,
Mike

== main.py ==
from subprocess import *

pInput = Popen('python subprogram.py', stdin=PIPE, shell=True).stdin
# pInput = Popen('subprogram.exe', stdin=PIPE, shell=True).stdin #
doesn't work

pInput.write('Data')
pInput.close()


== subprogram.py ==
import sys

input = sys.stdin.read() # Throws a bad descriptor exception.
print input
 
T

Thomas Heller

Mike Tammerman said:
Hi,

I want create a subprocess using Popen and pipe some input to it.
Although everything works perfectly while executing python in, it
doesn't work if I try with executables made by py2exe.

I think, stdin is invalidated if the program becomes an executable.
Because I get a "Bad file descriptor" exception in subprogram.py. I
will be more than apreciated, if any suggestions occur.

Thanks,
Mike

== main.py ==
from subprocess import *

pInput = Popen('python subprogram.py', stdin=PIPE, shell=True).stdin
# pInput = Popen('subprogram.exe', stdin=PIPE, shell=True).stdin #
doesn't work

pInput.write('Data')
pInput.close()


== subprogram.py ==
import sys

input = sys.stdin.read() # Throws a bad descriptor exception.
print input

Can it be that you're building a windows exe of subprogram.py? I get the
error you describe when I do that, for console programs it works -
both in the Python script and in the py2exe'd version.

This is, afaik, standard windows behaviour: GUI programs start with
stdin, stdout and stderr closed.

Thomas
 
M

Mike Tammerman

Yes, it throws exceptions if I build the exe of the subprogram.py.

So, is it possible to pipe some data to another py2exe'd application
without a console.

Mike
 
T

Thomas Heller

Mike Tammerman said:
Yes, it throws exceptions if I build the exe of the subprogram.py.

So, is it possible to pipe some data to another py2exe'd application
without a console.

I did this just some days ago. It required a little bit of
experimenting. This code executes the Windows XP ftp.exe console
program (but doesn't show a console), and reads its output. Here is the
relevant code snippet:

from subprocess import Popen, PIPE, STDOUT
# It seems subprocess doesn't open a console by default
# when run from a windows program
#
# Plus: For whatever reason, when running as py2exe'd GUI
# program, stdin=None doesn't work. We HAVE to specify PIPE for stdin,
# and we HAVE to use '... < NUL', and then stdin.close().
p = Popen("ftp.exe -s:upload.txt %s < NUL" % self._address,
cwd=os.path.join(util.get_main_dir(), "blah"),
shell=True,
stdin=PIPE,
stdout=PIPE,
stderr=STDOUT
)
p.stdin.close()
text = ""
while 1:
data = p.stdout.readline()
if not data:
break
<handle output data>
if p.wait():
<handle error>
return

The problem was that passing 'stdin=None' didn't work, for whatever
reason. I don't think it is a py2exe problem although the comment above
may suggest it.

Hope that helps,

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top