This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Popen(..., cwd=...) does not set PWD environment variable
Type: enhancement Stage: resolved
Components: Versions: Python 2.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: exarkun, gjb1002, pitrou, r.david.murray, ronaldoussoren, tebeka
Priority: normal Keywords:

Created on 2008-10-06 19:28 by tebeka, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
smime.p7s ronaldoussoren, 2009-11-27 07:23
Messages (9)
msg74388 - (view) Author: Miki Tebeka (tebeka) * Date: 2008-10-06 19:28
[21:26] pwd-bug $cat m
#!/usr/bin/env python

from subprocess import Popen
Popen(["./t"], cwd="./p")
[21:26] pwd-bug $cat p/t 
#!/usr/bin/env python

from os import environ
print environ["PWD"]
[21:26] pwd-bug $python m 
/Users/mtebeka/playground/pwd-bug

The output should be /Users/mtebeka/playground/pwd-bug/p
msg95747 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2009-11-26 16:02
In most shells $PWD is a magic variable that is maintained by the shell 
itself. 

IMHO python has no reason to mess with variables that happen to be defined 
by some shells. If subprocess were to set $PWD, os.setuid should set $UID, 
and there may be other magic variables as well.
msg95751 - (view) Author: Geoffrey Bache (gjb1002) Date: 2009-11-26 18:27
I can see your point, though I think particularly in this case it's
(unfortunately) fairly common that scripts on POSIX platforms read $PWD
instead of finding the current working directory properly. 

I'm probably not the first person that has had to set PWD explicitly in
a python program for this reason. Yes, it's really the fault of the
people who maintain the script I'm calling, but I don't think setting
PWD on POSIX could have any bad effects and should surely be easy to do?
msg95758 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2009-11-27 07:23
On 26 Nov, 2009, at 19:27, Geoffrey Bache wrote:

> 
> Geoffrey Bache <gjb1002@users.sourceforge.net> added the comment:
> 
> I can see your point, though I think particularly in this case it's
> (unfortunately) fairly common that scripts on POSIX platforms read $PWD
> instead of finding the current working directory properly. 
> 
> I'm probably not the first person that has had to set PWD explicitly in
> a python program for this reason. Yes, it's really the fault of the
> people who maintain the script I'm calling, but I don't think setting
> PWD on POSIX could have any bad effects and should surely be easy to do?

Shouldn't the script set $PWD itself? AFAIK shells like bash will set $PWD regardless of whether they are running as an interactive shell or as a shellscript.

Reading os.environ['PWD'] in a Python script not a good example of whey the proposed functionality might be useful because there are a number of ways to change the current working directory without affecting os.environ. For example using os.chdir, or even an extensions that calls the chdir system call directly.

Ronald
msg95773 - (view) Author: Geoffrey Bache (gjb1002) Date: 2009-11-27 18:42
You misunderstand: I am not reading $PWD. I need to call a C program as
a subprocess, which is written by a third party and which determines its
current working directory by reading $PWD. os.chdir will not have any
effect on this script, nor will passing "cwd" to "subprocess.call". I
have to write os.environ["PWD"] = os.getcwd() in my code before it will
work.

Not only that, but of course I have to know about $PWD and the fact that
some people use it. Otherwise it just seems like Python isn't correctly
passing on the current working directory.
msg95775 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-11-27 21:24
What if you pass shell=True to the subprocess call?
msg95840 - (view) Author: Geoffrey Bache (gjb1002) Date: 2009-11-30 19:45
I tried that and it didn't work, though not for this reason. I'm also
trying to read the output from the subprocess via a pipe and that wasn't
being collected for some reason. I didn't really track down why so far,
if it makes or breaks this bug I can do so though.
msg95841 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-11-30 19:50
`Popen` is the wrong place to implement this functionality.  It may be
reasonable to introduce a higher-level wrapper API which does this sort
of thing.  Consider that if `Popen` itself does it, though, then there
is no way to launch a process with a particular working directory
*without* PWD set in its environment.
msg95870 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2009-12-01 19:27
I agree that this is not something that Popen should be doing.  If you
need the environment variable set, the correct thing to do is what you
did: set it in the environment you pass to Popen.
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48307
2009-12-01 19:27:30r.david.murraysetstatus: open -> closed
priority: normal
type: enhancement


nosy: + r.david.murray
messages: + msg95870
resolution: rejected
stage: resolved
2009-11-30 19:50:21exarkunsetnosy: + exarkun
messages: + msg95841
2009-11-30 19:45:20gjb1002setmessages: + msg95840
2009-11-27 21:24:32pitrousetnosy: + pitrou
messages: + msg95775
2009-11-27 18:42:43gjb1002setmessages: + msg95773
2009-11-27 07:23:02ronaldoussorensetfiles: + smime.p7s

messages: + msg95758
2009-11-26 18:27:44gjb1002setmessages: + msg95751
2009-11-26 16:02:30ronaldoussorensetnosy: + ronaldoussoren
messages: + msg95747
2009-11-26 10:21:12gjb1002setnosy: + gjb1002
2008-10-06 19:28:02tebekacreate