subprocess.Popen(..., cwd=...) and Makefile $(PWD) don't play nice

Discussion in 'Python' started by Miki, Oct 6, 2008.

  1. Miki

    Miki Guest

    Hello All,

    Can anybody explain why Makefile $(PWD) does show the right directory
    when running under subprocess.Popen(..., cwd=...)

    For example:
    [18:07] tmp $cat /tmp/p/Makefile
    all:
    @echo $(PWD)
    [18:07] tmp $cat t
    #!/usr/bin/env python

    from subprocess import Popen
    Popen(["make"], cwd="/tmp/p")

    [18:07] tmp $./t
    /tmp

    The output of the "make" command should have been "/tmp/p" and not "/
    tmp"

    TIA,
    --
    Miki <>
    http://pythonwise.blogspot.com
     
    Miki, Oct 6, 2008
    #1
    1. Advertising

  2. "Gabriel Genellina" <> writes:

    > En Mon, 06 Oct 2008 13:09:26 -0300, Miki <> escribió:


    >> Can anybody explain why Makefile $(PWD) does show the right directory
    >> when running under subprocess.Popen(..., cwd=...)
    >>
    >> For example:
    >> [18:07] tmp $cat /tmp/p/Makefile
    >> all:
    >> @echo $(PWD)
    >> [18:07] tmp $cat t
    >> #!/usr/bin/env python
    >>
    >> from subprocess import Popen
    >> Popen(["make"], cwd="/tmp/p")
    >>
    >> [18:07] tmp $./t
    >> /tmp
    >>
    >> The output of the "make" command should have been "/tmp/p" and not "/
    >> tmp"


    > I've tried the equivalent version on Windows and it worked fine.
    > And I can't see how make could find the Makefile at /tmp/p if the current
    > directory were another one... (?)


    The problem is, he is not printing the name of the current working
    directory; he is printing the value of the variable $PWD. That is
    likely set from the environment by the shell he started the Python
    program from, but Python does *not* update to reflect changes to
    the working directory.

    Use '@pwd' instead of '@echo $(PWD)'.


    --
    Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
    "Beware of bugs in the above code; I have ! bellman @ lysator.liu.se
    only proved it correct, not tried it." ! Make Love -- Nicht Wahr!
     
    Thomas Bellman, Oct 7, 2008
    #2
    1. Advertising

  3. En Mon, 06 Oct 2008 13:09:26 -0300, Miki <> escribió:

    > Can anybody explain why Makefile $(PWD) does show the right directory
    > when running under subprocess.Popen(..., cwd=...)
    >
    > For example:
    > [18:07] tmp $cat /tmp/p/Makefile
    > all:
    > @echo $(PWD)
    > [18:07] tmp $cat t
    > #!/usr/bin/env python
    >
    > from subprocess import Popen
    > Popen(["make"], cwd="/tmp/p")
    >
    > [18:07] tmp $./t
    > /tmp
    >
    > The output of the "make" command should have been "/tmp/p" and not "/
    > tmp"


    I've tried the equivalent version on Windows and it worked fine.
    And I can't see how make could find the Makefile at /tmp/p if the current
    directory were another one... (?)

    --
    Gabriel Genellina
     
    Gabriel Genellina, Oct 7, 2008
    #3
  4. Miki

    Miki Guest

    Re: subprocess.Popen(..., cwd=...) and Makefile $(PWD) don't playnice

    > The problem is, he is not printing the name of the current working
    > directory; he is printing the value of the variable $PWD.  That is
    > likely set from the environment by the shell he started the Python
    > program from, but Python does *not* update to reflect changes to
    > the working directory.

    Should I see it as a bug? Should subprocess change this?

    > Use '@pwd' instead of '@echo $(PWD)'.

    I'm using '$(shell pwd)', just wondered why $PWD didn't work.

    Thanks for the explanation.
    --
    Miki <>
    http://pythonwise.blogspot.com
     
    Miki, Oct 13, 2008
    #4
  5. Miki <> writes:

    >> The problem is, he is not printing the name of the current working
    >> directory; he is printing the value of the variable $PWD. =A0That is
    >> likely set from the environment by the shell he started the Python
    >> program from, but Python does *not* update to reflect changes to
    >> the working directory.


    > Should I see it as a bug? Should subprocess change this?


    No, it's not a bug. No, subprocess should not muck around with
    the environment variables unless explicitly told to do so.

    $PWD is only set by some (not all!) shells. It should not be
    trusted outside those shells, and hardly inside them either.


    --
    Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
    "Adde parvum parvo magnus acervus erit" ! bellman @ lysator.liu.se
    (From The Mythical Man-Month) ! Make Love -- Nicht Wahr!
     
    Thomas Bellman, Oct 14, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Black
    Replies:
    2
    Views:
    447
    Steve Christensen
    Sep 9, 2004
  2. Paul Rubin
    Replies:
    1
    Views:
    288
    Skip Montanaro
    May 14, 2005
  3. Ivan Vinogradov

    subprocess cwd keyword.

    Ivan Vinogradov, Oct 26, 2006, in forum: Python
    Replies:
    3
    Views:
    346
    Ivan Vinogradov
    Oct 27, 2006
  4. hyperboreean

    decorators don't play nice with nose?

    hyperboreean, Apr 6, 2009, in forum: Python
    Replies:
    3
    Views:
    690
    Michele Simionato
    Apr 7, 2009
  5. Thauhtopa
    Replies:
    4
    Views:
    290
    Thauhtopa
    Dec 7, 2004
Loading...

Share This Page