0

I am trying to access a path variable using python.

Subprocess.call & Subprocess.Popen seem unable to do so, despite the fact that the Exe's location is in my PATH Variables, and can be run from cmd.exe I can also run this with an absolute path to the executable, but this isn't desirable.

Managed to find this article: Subprocess.call or Subprocess.Popen cannot use executables that are in PATH (Linux/Windows)

But their solution to grab the environment variables and use them didn't work for me.

import os
import subprocess

environment_vars = os.environ

input_image_location = 'D:/python/images/test_image.bmp'
output_image_location = 'D:/python/images/test_image_output.ps'

cmd_args = ["potrace", "-p", input_image_location, "-o",output_image_location]

proc = subprocess.Popen(cmd_args, env=environment_vars)

But this is erroring with:

File "C:Python27\lib\subprocess.py", line 958, in _execute_child 
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Any tips on how to get this to work?

Community
  • 1
  • 1
APF
  • 1
  • 2
  • What's the extension of "potrace"? `Popen` calls Windows `CreateProcess`, which only tries to append ".EXE", whereas cmd.exe tries all of the extensions in `PATHEXT`. – Eryk Sun Jan 06 '16 at 03:48
  • In the linked question, [joshua's answer](http://stackoverflow.com/a/17113728) may be relevant. cmd.exe ignores quotes around directories in `PATH` when searching for files, but `CreateProcess` treats the quotes as literally part of the directory name. Directories should never be quoted in `PATH`. – Eryk Sun Jan 06 '16 at 04:05
  • BTW, setting `env=os.environ` should make no difference for the child process if the current process environment is in sync with `os.environ`. A child process defaults to inheriting a copy of the parent's environment. Also, the executable is found in the context of the current process; the environment passed to the child process is irrelevant unless you pass `shell=True`, in which case you're changing the environment of the cmd.exe instance. – Eryk Sun Jan 06 '16 at 04:06
  • Thanks for the speedy reply! The extension for potrace is .EXE. The absolute path works no worries: 'C:/Python27/MODULES/potrace-1.13.win64/potrace.exe'. Also did see that comment you mentioned, and went back to check that but I didn't find any quotes to be removed. The PATH Variable is: C:\Python27\MODULES\potrace-1.13.win64\ – APF Jan 06 '16 at 04:46
  • That's the exact value of `print(os.environ['PATH'])`? Try this: `from ctypes import *;` `path = (c_wchar * 32768)();` `windll.kernel32.GetEnvironmentVariableW(u'PATH', path, 32768);` `print(path.value)`. – Eryk Sun Jan 06 '16 at 05:32
  • D:\pythonxy\console C:\Program Files (x86)\pythonxy\swig D:\pythonxy\gettext\bin C:\Python27\MODULES\potrace-1.13.win64\potrace.exe D:\Python\Scripts\ D:\Python\ C:\Users\Alex\AppData\Local\Programs\Python\Launcher\ There was more, but this is the cut down version, with potrace.exe linked – APF Jan 06 '16 at 06:02
  • 1
    Your `PATH` contains the fully-qualified path of the executable: `C:\Python27\MODULES\potrace-1.13.win64\potrace.exe`. It should only contain the directory: `C:\Python27\MODULES\potrace-1.13.win64`. – Eryk Sun Jan 06 '16 at 06:18
  • Hi Eryksun, That seems to have fixed it. Checked my path variable again, and it was the correct, but had not taken effect it seems. did a restart and it was all good. Thanks! – APF Jan 07 '16 at 00:11

0 Answers0