You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling 'python' via subprocess.call ignoring passed %PATH% #68981
I just noticed that there is a litte inconvenience when I try to invoke 'python' via subprocess.call passing an environment (%PATH%) from a script. I pass an environment where %PATH% only contains one directory where a python2.7.3-exe is present (I checked with "subprocess.call(['where','python'],env=environment)" that python is found there). However when calling "subprocess.call(['python'],env=environment)" python 2.7.9 is opened, which is the python version I called the script with. Seems inconvenient to me.
This is standard Windows behaviour. Executables are always located first in the directory where your program (in this case the Python executable) is running from - even before searching PATH.
If you want to use a different Python, you should specify the full path to the executable - "subprocess.call(["C:\\Python34\\python.exe"])".
Popen calls Windows CreateProcess. If the Popen "executable" argument isn't used or if the file from the command line doesn't include a directory path, then CreateProcess searches for it in the following directories:
1. The directory from which the application loaded.
2. The current directory for the parent process.
* This step is skipped if the parent defines
%NoDefaultCurrentDirectoryInExePath%.
3. The 32-bit Windows system directory.
4. The 16-bit Windows system directory.
5. The Windows directory.
6. The directories that are listed in the PATH
environment variable.
Also note that the PATH searched is that of the *parent* process; the "env" parameter doesn't affect this.
Call the SearchPath function if you need a custom search for the fully qualified path.
Activity
phbarnacle commentedon Aug 5, 2015
I just noticed that there is a litte inconvenience when I try to invoke 'python' via subprocess.call passing an environment (%PATH%) from a script. I pass an environment where %PATH% only contains one directory where a python2.7.3-exe is present (I checked with "subprocess.call(['where','python'],env=environment)" that python is found there). However when calling "subprocess.call(['python'],env=environment)" python 2.7.9 is opened, which is the python version I called the script with. Seems inconvenient to me.
Greetings
pfmoore commentedon Aug 5, 2015
This is standard Windows behaviour. Executables are always located first in the directory where your program (in this case the Python executable) is running from - even before searching PATH.
If you want to use a different Python, you should specify the full path to the executable - "subprocess.call(["C:\\Python34\\python.exe"])".
eryksun commentedon Aug 5, 2015
Popen calls Windows CreateProcess. If the Popen "executable" argument isn't used or if the file from the command line doesn't include a directory path, then CreateProcess searches for it in the following directories:
Also note that the PATH searched is that of the *parent* process; the "env" parameter doesn't affect this.
Call the SearchPath function if you need a custom search for the fully qualified path.
For example:
>>> search_path('python') u'C:\\Program Files\\Python27\\python.exe' >>> import os >>> search_path('python', os.environ['PATH']) u'' >>> search_path('more.com', os.environ['PATH']) u'C:\\Windows\\System32\\more.com' >>> search_path('more', ext='.com') u'C:\\Windows\\system32\\more.com'