-
-
Notifications
You must be signed in to change notification settings - Fork 29.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python does not expand env["PATH"] properly in the subprocess module when using the env keyword. #105889
Comments
On Windows,
|
Note that the value of env = os.environ.copy()
pwsh_path = r"C:\Program Files\WindowsApps\Microsoft.PowerShell_7.3.3.0_x64__8wekyb3d8bbwe"
env["PATH"] = fr"{env['PATH']}{os.pathsep}{pwsh_path}" Off topic comment While directly executing r"C:\Program Files\WindowsApps\Microsoft.PowerShell_7.3.3.0_x64__8wekyb3d8bbwe\pwsh.exe" happens to work in this case, even without enabling the "pwsh.exe" app execution alias, it's an unusual way to execute an app. The app files under "%ProgramFiles%\WindowsApps" aren't directly executable by standard users or even administrators (i.e. internally the initial |
First, thanks for the fast, detailed and good answer!
To make sure I fully got your answer: What I want to achieve (i.e., the subprocess knowing the updated value for Seems a bad question based on your answer and the Python docs you linked, but still: Is there a solution/workaround you could think of to make that possible what I want to achieve without using Thanks for the explanation on the WindowsApps directory. |
If you modify the value of The difference compared to POSIX is partially due to the fact that creating a child process is fundamentally different on a platform such as Windows that doesn't use a combination of That said, Microsoft could have implemented |
Ah now I think I get the full picture. So what I basically need to do is: import os
import shutil
import subprocess
new_env = os.environ.copy()
path_1 = r"C:\foo" # contains a "a.exe"
path_2 = r"C:\bla" # contains a "b.exe"; "a.exe" also needs to know about "b.exe" when run
new_env["PATH"] = fr"{env['PATH']}{os.pathsep}{path_1}{os.pathsep}{path_2}"
a_exe = shutil.which("a", path=new_env["PATH"])
# the subprocess can now work, as we provide an absolute path to the executable
# to run, and then the started a_exe process is able to find b.exe on its path
# provided through the 'env'.
subprocess.run([a_exe,"some", "args"], env=new_env) Is my understanding correct? And, I need to say it again, thanks for your answers, so detailed, so good. |
Yes, search for the executable using Note that on Windows |
Bug report
Python does not expand
env["PATH"]
properly in thesubprocess
module when using theenv
keyword.Consider the following examples:
which will print your current PATH.
Now, we add an environment variable
BLA=FOO
and run it againOk, so let's update PATH to include some additional directory.
Ok, so let's now let's check
shutil.which()
s opinion on that.So far so good. Let's create a non-Python subprocess with an updated env, and see what happens:
The created subprocess does not respect the provided key
PATH
in theenv
dictionary for theenv
argument in the last example, but respects it in the second and third last example.This behavior is not documented. So this seems to me like a bug.
Of course, if setting
shell=True
then the last example also works, as the environment variables are then expanded. But the last example should work without the need forshell=True
.Your environment
The text was updated successfully, but these errors were encountered: