stdin and py2exe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Tammerman

    #1

    stdin and py2exe

    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).std in
    # pInput = Popen('subprogr am.exe', stdin=PIPE, shell=True).std in #
    doesn't work

    pInput.write('D ata')
    pInput.close()


    == subprogram.py ==
    import sys

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

  • Thomas Heller

    #2
    Re: stdin and py2exe

    "Mike Tammerman" <mtammerman@gma il.com> writes:
    [color=blue]
    > 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).std in
    > # pInput = Popen('subprogr am.exe', stdin=PIPE, shell=True).std in #
    > doesn't work
    >
    > pInput.write('D ata')
    > pInput.close()
    >
    >
    > == subprogram.py ==
    > import sys
    >
    > input = sys.stdin.read( ) # Throws a bad descriptor exception.
    > print input[/color]

    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

    Comment

    • Mike Tammerman

      #3
      Re: stdin and py2exe

      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

      Comment

      • Thomas Heller

        #4
        Re: stdin and py2exe

        "Mike Tammerman" <mtammerman@gma il.com> writes:
        [color=blue]
        > 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.[/color]

        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.joi n(util.get_main _dir(), "blah"),
        shell=True,
        stdin=PIPE,
        stdout=PIPE,
        stderr=STDOUT
        )
        p.stdin.close()
        text = ""
        while 1:
        data = p.stdout.readli ne()
        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

        Comment

        Working...