os.popen3 with windows; help?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell E. Owen

    os.popen3 with windows; help?

    I'm trying to launch an application from Python 2.3 on Windows. The
    application is "ds9" (an image viewer), and is installed in C:\Program
    Files\ds9\ds9

    On unix I just do:
    os.popen3("ds9" )
    and close the returned files and all is good. (I'm not trying to
    communicate with the program via popen3 and so had been using
    os.spawnlp, but that doesn't exist on Windows.)

    On Windows os.popen3("ds9" ) does nothing, and the stderr returned from
    os.popen3 has a message saying the program is unknown.

    So I tried being specific:
    os.popen3("C:\\ Program Files\\ds9\\ds9 ")
    This also fails and the program that is not found is "C:\Program ",
    suggesting that the space in "Program Files" is causing the problem. The
    following failed in exactly the same way:
    os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")

    Any suggestions?

    -- Russell

    P.S. I installed the app using its Windows binary installer, which
    unpacks a few files in C:\\Program files\. Windows doesn't seem to know
    the program exists; it's not in the task bar, for example. But one can
    double-click it to run it.
  • Sylvain Defresne

    #2
    Re: os.popen3 with windows; help?

    > So I tried being specific:[color=blue]
    > os.popen3("C:\\ Program Files\\ds9\\ds9 ")
    > This also fails and the program that is not found is "C:\Program ",
    > suggesting that the space in "Program Files" is causing the problem. The
    > following failed in exactly the same way:
    > os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")
    >
    > Any suggestions?[/color]

    I don't have a windows to test, but does the following works (that is
    protecting the space from the shell by enclosing the path in double
    quote) ?

    os.popen3('"C:\ \Program Files\\ds9\\ds9 .exe"')

    --
    keiichi

    Comment

    • Sean Blakey

      #3
      Re: os.popen3 with windows; help?

      On Tue, 26 Oct 2004 00:41:39 +0200, "@gmail.com Sylvain Defresne
      <"sdefresne> wrote:[color=blue][color=green]
      > > So I tried being specific:
      > > os.popen3("C:\\ Program Files\\ds9\\ds9 ")
      > > This also fails and the program that is not found is "C:\Program ",
      > > suggesting that the space in "Program Files" is causing the problem. The
      > > following failed in exactly the same way:
      > > os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")
      > >
      > > Any suggestions?[/color]
      >
      > I don't have a windows to test, but does the following works (that is
      > protecting the space from the shell by enclosing the path in double
      > quote) ?
      >
      > os.popen3('"C:\ \Program Files\\ds9\\ds9 .exe"')
      >
      > --
      > keiichi
      >
      >
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]


      --
      Sean Blakey
      Saint of Mild Amusement, Evil Genius, Big Geek
      Python/Java/C++/C(Unix/Windows/Palm/Web) developer
      email: pythonista@gmai l.com
      quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])

      Comment

      • Lad

        #4
        Re: os.popen3 with windows; help?

        "Russell E. Owen" <rowen@cesmail. net> wrote in message news:<rowen-1B99F3.15314025 102004@gnus01.u .washington.edu >...[color=blue]
        > I'm trying to launch an application from Python 2.3 on Windows. The
        > application is "ds9" (an image viewer), and is installed in C:\Program
        > Files\ds9\ds9
        >
        > On unix I just do:
        > os.popen3("ds9" )
        > and close the returned files and all is good. (I'm not trying to
        > communicate with the program via popen3 and so had been using
        > os.spawnlp, but that doesn't exist on Windows.)
        >
        > On Windows os.popen3("ds9" ) does nothing, and the stderr returned from
        > os.popen3 has a message saying the program is unknown.
        >
        > So I tried being specific:
        > os.popen3("C:\\ Program Files\\ds9\\ds9 ")
        > This also fails and the program that is not found is "C:\Program ",
        > suggesting that the space in "Program Files" is causing the problem. The
        > following failed in exactly the same way:
        > os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")
        >
        > Any suggestions?
        >
        > -- Russell
        >
        > P.S. I installed the app using its Windows binary installer, which
        > unpacks a few files in C:\\Program files\. Windows doesn't seem to know
        > the program exists; it's not in the task bar, for example. But one can
        > double-click it to run it.[/color]

        I think the problem is in long name folder: Program Files. So, try

        Progra~1 instead of Program Files

        Comment

        • Patrick L. Nolan

          #5
          Re: os.popen3 with windows; help?

          Lad <export@hope.cz > wrote:[color=blue]
          > "Russell E. Owen" <rowen@cesmail. net> wrote in message news:<rowen-1B99F3.15314025 102004@gnus01.u .washington.edu >...[color=green]
          >> I'm trying to launch an application from Python 2.3 on Windows. The
          >> application is "ds9" (an image viewer), and is installed in C:\Program
          >> Files\ds9\ds9
          >>
          >> On unix I just do:
          >> os.popen3("ds9" )
          >> and close the returned files and all is good. (I'm not trying to
          >> communicate with the program via popen3 and so had been using
          >> os.spawnlp, but that doesn't exist on Windows.)
          >>
          >> On Windows os.popen3("ds9" ) does nothing, and the stderr returned from
          >> os.popen3 has a message saying the program is unknown.
          >>
          >> So I tried being specific:
          >> os.popen3("C:\\ Program Files\\ds9\\ds9 ")
          >> This also fails and the program that is not found is "C:\Program ",
          >> suggesting that the space in "Program Files" is causing the problem. The
          >> following failed in exactly the same way:
          >> os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")
          >>
          >> Any suggestions?
          >>
          >> -- Russell
          >>
          >> P.S. I installed the app using its Windows binary installer, which
          >> unpacks a few files in C:\\Program files\. Windows doesn't seem to know
          >> the program exists; it's not in the task bar, for example. But one can
          >> double-click it to run it.[/color][/color]
          [color=blue]
          > I think the problem is in long name folder: Program Files. So, try[/color]
          [color=blue]
          > Progra~1 instead of Program Files[/color]

          An automatic way to accomplish this is

          import win32api
          shortpath = win32api.GetSho rtPathName(path )
          os.popen3(short path)

          Also, I have found by bitter experience that ds9 is unable to open any
          file whose path includes a space. Put your data files somewhere else.

          --
          * Patrick L. Nolan *
          * W. W. Hansen Experimental Physics Laboratory (HEPL) *
          * Stanford University *

          Comment

          • Tony C

            #6
            Re: os.popen3 with windows; help?

            "Russell E. Owen" <rowen@cesmail. net> wrote in message news:<rowen-1B99F3.15314025 102004@gnus01.u .washington.edu >...[color=blue]
            > I'm trying to launch an application from Python 2.3 on Windows. The
            > application is "ds9" (an image viewer), and is installed in C:\Program
            > Files\ds9\ds9[/color]
            [color=blue]
            > On Windows os.popen3("ds9" ) does nothing, and the stderr returned from[/color]

            This won't work unless the installer for DS9 or someone manually put
            the directory where DS9 is installed, into the path.


            for long paths try using raw strings as in
            os.popen3(r"C:\ My Long Dir\My Long Program\MyProg. exe")

            Comment

            • Michele Petrazzo

              #7
              Re: os.popen3 with windows; help?

              Russell E. Owen wrote:[color=blue]
              > So I tried being specific:
              > os.popen3("C:\\ Program Files\\ds9\\ds9 ")
              > This also fails and the program that is not found is "C:\Program ",
              > suggesting that the space in "Program Files" is causing the problem. The
              > following failed in exactly the same way:
              > os.popen3("C:\\ Program\ Files\\ds9\\ds9 ")
              >
              > Any suggestions?
              >[/color]

              The problem is that there are spaces into the path. Try to user
              os.popen3('"C:\ \Program\ Files\\ds9\\ds9 "').
              The better choice is:

              path_complete = os.path.join('c :\\', 'Program Files', 'ds', 'ds9')
              os.popen3('"'+ path_complete +'"' )

              It work for me.

              Michele

              Comment

              • Russell E. Owen

                #8
                Re: os.popen3 with windows; help?

                In article <cllv09$kv9$1@n ews.Stanford.ED U>,
                "Patrick L. Nolan" <pln@cosmic.sta nford.edu> wrote:
                [color=blue]
                >Lad <export@hope.cz > wrote:[color=green]
                >> "Russell E. Owen" <rowen@cesmail. net> wrote in message
                >> news:<rowen-1B99F3.15314025 102004@gnus01.u .washington.edu >...[color=darkred]
                >>> I'm trying to launch an application from Python 2.3 on Windows. The
                >>> application is "ds9" (an image viewer), and is installed in C:\Program
                >>> Files\ds9\ds9
                >>>...[/color]
                >> I think the problem is in long name folder: Program Files. So, try[/color]
                >[color=green]
                >> Progra~1 instead of Program Files[/color]
                >
                >An automatic way to accomplish this is
                >
                >import win32api
                >shortpath = win32api.GetSho rtPathName(path )
                >os.popen3(shor tpath)
                >
                >Also, I have found by bitter experience that ds9 is unable to open any
                >file whose path includes a space. Put your data files somewhere else.[/color]

                Thanks to everyone for their replies.

                Lad and Patrick's suggestions both worked perfectly. (Simply trying to
                protect the path with an extra set of quotes, as suggested by one kind
                non-Windows-savvy soul, did not work)..

                I also tested the bug mentioned by Patrick. I put a fits image file into
                a directory whose name contained a space and tried to open that file in
                ds9. it worked fine. I hope this means that the problem with spaces in
                names has been thoroughly fixed.

                Tip for ds9 and xpa users (xpa is a communication protocol used by ds9):
                I learned from a ds9 developer that a proper installation of ds9 and
                should have the xpa files in the same directory as ds9. This is NOT how
                the binary ds9 and xpa installers do it, but it is easy to fix after the
                installation. Unless this is done, ds9 cannot register itself with xpa,
                making communication needlessly messy.


                One more question:
                - I noticed that when I use os.popen3 to open ds9, i have to keep the
                returned reference to stdout around as long as ds9 itself is open or
                Python hangs. I can't close it. I can't lose the reference and allow it
                to be garbage-collected. Any suggestions for avoiding this? (At least it
                doesn't prevent Python from shutting down when requested.)

                I had been using os.spawn... to launch ds9, but spawn is not supported
                on Windows, alas. I communicate with ds9 via xpa, so the pipes returned
                by popen3 are of no interest.

                -- Russell

                Comment

                • Russell E. Owen

                  #9
                  Re: os.popen3 with windows; help?

                  In article <rowen-E1A6D6.10270727 102004@gnus01.u .washington.edu >,
                  "Russell E. Owen" <rowen@cesmail. net> wrote:
                  [color=blue]
                  >One more question:
                  >- I noticed that when I use os.popen3 to open ds9, i have to keep the
                  >returned reference to stdout around as long as ds9 itself is open or
                  >Python hangs. I can't close it. I can't lose the reference and allow it
                  >to be garbage-collected. Any suggestions for avoiding this? (At least it
                  >doesn't prevent Python from shutting down when requested.)
                  >
                  >I had been using os.spawn... to launch ds9, but spawn is not supported
                  >on Windows, alas. I communicate with ds9 via xpa, so the pipes returned
                  >by popen3 are of no interest.[/color]

                  Sorry, that was a stupid question. I had been using spawnlp and hadn't
                  noticed the little footnote in the docs about the spawn variants that
                  aren't supported under Windows. All I have to do is switch to a version
                  of spawn that IS cross-platform.

                  So. Case closed. Thanks again for everybody's help!

                  -- Russell

                  Comment

                  • Tony C

                    #10
                    Re: os.popen3 with windows; help?

                    Michele Petrazzo <michele.petraz zo@TOGLIunipex. it> wrote in message[color=blue]
                    > The problem is that there are spaces into the path. Try to user
                    > os.popen3('"C:\ \Program\ Files\\ds9\\ds9 "').
                    > The better choice is:
                    >
                    > path_complete = os.path.join('c :\\', 'Program Files', 'ds', 'ds9')
                    > os.popen3('"'+ path_complete +'"' )
                    >
                    > It work for me.
                    >
                    > Michele[/color]

                    Try using raw strings, as I had posted in my reply.
                    Then you don't have to mess with joining strings, or using double
                    backslashes, or qoutes within quotes.
                    [color=blue][color=green]
                    >>os.popen3(r"C :\Program\ Files\ds9\ds9")[/color][/color]

                    Comment

                    • Russell E. Owen

                      #11
                      Re: os.popen3 with windows; help?

                      In article <8249c4ac.04102 81110.39d68dc2@ posting.google. com>,
                      cappy2112@gmail .com (Tony C) wrote:
                      [color=blue]
                      >Michele Petrazzo <michele.petraz zo@TOGLIunipex. it> wrote in message[color=green]
                      >> The problem is that there are spaces into the path. Try to user
                      >> os.popen3('"C:\ \Program\ Files\\ds9\\ds9 "').
                      >> The better choice is:
                      >>
                      >> path_complete = os.path.join('c :\\', 'Program Files', 'ds', 'ds9')
                      >> os.popen3('"'+ path_complete +'"' )
                      >>
                      >> It work for me.
                      >>
                      >> Michele[/color]
                      >
                      >Try using raw strings, as I had posted in my reply.
                      >Then you don't have to mess with joining strings, or using double
                      >backslashes, or qoutes within quotes.
                      >[color=green][color=darkred]
                      >>>os.popen3(r" C:\Program\ Files\ds9\ds9")[/color][/color][/color]

                      I find this fails the same way as not using r"..." and doubling the
                      backslashes. Windows thinks the path to the executable stops at the
                      space. (I tried os.popen3(r"C:\ Program Files\ds9\ds9") and the less
                      likely variant os.popen3(r"C:\ Program\ Files\ds9\ds9") )

                      More info for anyone still following this thread:
                      The new subprocess module has promise here as a replacement for
                      os.popen3, since it allows a separate executable argument. Unfortunately
                      my tests indicate that executable and commands-as-strings (instead of
                      lists) aren't compatible; I've reported this as bug 1056441
                      <http://sourceforge.net/tracker/index...056441&group_i
                      d=5470&atid=105 470> for anyone who wants details.

                      I could recode to use commands-as-lists. This works, but is a big
                      headache for interactive use (one of the main targets for this module).

                      Also, for ds9 or xpa users:
                      - On Windows: ds9 will not connect to xpa unless the current directory
                      includes xpans or xpans is already running. Yecch. I ended up using
                      os.chdir before spawning ds9, then changing back.

                      - spaces in file names ARE bad news (as somebody warned me). ds9 can
                      open files with spaces in their names using the File menu, but not using
                      xpa. If the file name has a space you are totally out of luck. If the
                      path has a space (but not the file name) then you can try various tricks:
                      - quote the path in '"..."' (single quotes on the outside; no other form
                      of double quoting I tried worked). Fails on Windows.
                      - backslash-escape the spaces. Fails on Windows, of course.
                      - chdir to the file's directory first, then open, then chdir back. But
                      you can't be in two places at once. If you use chdir to get around the
                      os.popen3 problem of the executable having a space in its path name then
                      you can't also use it to get around the other problem.

                      Sigh.

                      -- Russell

                      Comment

                      • Anthony

                        #12
                        Re: os.popen3 with windows; help?

                        On Thu, 28 Oct 2004 15:58:34 -0700, Russell E. Owen <rowen@cesmail. net> wrote:[color=blue][color=green][color=darkred]
                        >>>>os.popen3(r "C:\Program \ Files\ds9\ds9")[/color][/color]
                        >
                        > I find this fails the same way as not using r"..." and doubling the
                        > backslashes. Windows thinks the path to the executable stops at the
                        > space. (I tried os.popen3(r"C:\ Program Files\ds9\ds9") and the less
                        > likely variant os.popen3(r"C:\ Program\ Files\ds9\ds9") )[/color]

                        Have you tried a wildcard? ie. something like:

                        os.popen3(r"C:\ Program*\ds9\ds 9"))

                        Bit of a hack though :)

                        Anthony

                        --
                        -----------------------------------------------------
                        HyPEraCtiVE? HeY, WhO aRE YoU cALliNg HypERaCtIve?!
                        aNthONy.BrIGgS@ gmAiL.CoM
                        -----------------------------------------------------

                        Comment

                        • Josiah Carlson

                          #13
                          Re: os.popen3 with windows; help?


                          "Russell E. Owen" <rowen@cesmail. net> wrote:[color=blue]
                          > - quote the path in '"..."' (single quotes on the outside; no other form
                          > of double quoting I tried worked). Fails on Windows.[/color]

                          Which windows? It works for me on both 2k and XP (though I'm not
                          running ds9):[color=blue][color=green][color=darkred]
                          >>> os.popen3('"C:\ \Program Files\\WinImage \\winimage.exe" ')[/color][/color][/color]
                          (<open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'w' at 0x007E7AE0>,
                          <open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'r' at 0x007EC2E0>,
                          <open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'r' at 0x007EC360>)


                          - Josiah

                          Comment

                          • Russell E. Owen

                            #14
                            Re: os.popen3 with windows; help?

                            In article <mailman.5676.1 099074998.5135. python-list@python.org >,
                            Josiah Carlson <jcarlson@uci.e du> wrote:
                            [color=blue]
                            >"Russell E. Owen" <rowen@cesmail. net> wrote:[color=green]
                            >> - quote the path in '"..."' (single quotes on the outside; no other form
                            >> of double quoting I tried worked). Fails on Windows.[/color]
                            >
                            >Which windows? It works for me on both 2k and XP (though I'm not
                            >running ds9):[color=green][color=darkred]
                            >>>> os.popen3('"C:\ \Program Files\\WinImage \\winimage.exe" ')[/color][/color]
                            >(<open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'w' at
                            >0x007E7AE0>,
                            > <open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'r' at
                            > 0x007EC2E0>,
                            > <open file '"C:\Program Files\WinImage\ winimage.exe"', mode 'r' at
                            > 0x007EC360>)[/color]

                            You are right!

                            Putting double quotes around the path to the executable works so long as
                            there are no command line arguments. Unfortunately, as soon as you add
                            extra words, os.popen3 acts as if the double quotes are missing and can
                            no longer find the executable.

                            This works:
                            os.popen3('"C:\ \Program Files\\ds9\\ds9 "')

                            This fails on Windows -- cannot launch C:\Program -- but I tried
                            equivalent code on unix and it works fine (not that one normally needs
                            it on unix; executables are usually found automatically):
                            os.popen3('"C:\ \Program Files\\ds9\\ds9 " -title foo -port 0')

                            I see this same bug trying to use the subprocess module in Python 2.4b1
                            with my Windows Python 2.3 (see PR 105470:
                            <http://sourceforge.net/tracker/index...057048&group_i
                            d=5470&atid=105 470>).

                            My current solution is to temporarily change to the executable's
                            directory, for example:
                            currDir = os.getcwd()
                            try:
                            os.chdir(_XPADi r)
                            fds = os.popen3("xpag et ds9 file myfile.fits")
                            finally:
                            os.chdir(currDi r)

                            *****

                            Regarding my statement you quoted:
                            - quote the path in '"..."' (single quotes on the outside; no other form
                            of double quoting I tried worked). Fails on Windows.

                            That was in reference to using xpa to send file names to ds9. The file
                            name is sent as a command line argument, so it's a different issue than
                            discussed above. For those who care, it turns out that "{...}" is the
                            recommended way to send such paths to ds9. That's fine, but it also
                            turns out that ds9 on Windows requires the directory separator to be /,
                            not \!

                            For any ds9/xpa users out there, I posted a summary what I learned:
                            "Tricks for Using xpa to Command ds9":
                            <http://www.astro.washi ngton.edu/rowen/ds9andxpa.html> . I'll keep this
                            updated if I learn anything new. I also hope to shorten it if some of
                            the ds9 bugs and quirks are fixed. I do not discuss the details of
                            os.popen3 and such, just xpa/ds9 issues.

                            Thank you all again for all your help. I hope to have a cross-platform
                            version of RO.DS9 out in the next week or so.

                            -- Russell

                            Comment

                            Working...