By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

stdin and py2exe

P: n/a
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

Sep 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mike Tammerman" <mt********@gmail.com> writes:
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
Sep 14 '05 #2

P: n/a
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

Sep 14 '05 #3

P: n/a
"Mike Tammerman" <mt********@gmail.com> writes:
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
Sep 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.