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

M

Miki

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,
 
T

Thomas Bellman

Gabriel Genellina said:
En Mon, 06 Oct 2008 13:09:26 -0300, Miki <[email protected]> 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)'.
 
G

Gabriel Genellina

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... (?)
 
M

Miki

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.
 
T

Thomas Bellman

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.
 

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,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top