4

I have a script which won't run inside of Nuke's built-in Python interpreter, so I'm trying to launch in via the system's Python instead. I'm using subprocess.Popen to do this, but it still won't run inside a subprocess (missing modules), even though it runs fine at the command prompt (cmd.exe)

I think the problem is, that I just don't understand what environment my subprocess starts up in. It doesn't even run the expected version of Python.

In cmd.exe:

C:/Python27/python.exe -V

> Python 2.7.16

In Nuke:

import subprocess as sub
print sub.Popen("C:/Python27/python.exe -V", stderr=sub.PIPE).communicate()[1]

# Result: Python 2.7.13

print sub.Popen('C:/Python27/python.exe -c 
                "import sys;print(sys.executable)"',
                stdout=sub.PIPE).communicate()[0]

# Result: C:\Python27\python.exe

Where is this lower version of Python coming from? It's probably not a coincidence that Nuke's built-in Python is also 2.7.13, but why would Popen run that and not the .exe I'm specifying?

Note: same result whether I give shell=True or shell=False

Spacey
  • 153
  • 1
  • 7
  • Check `sub.Popen('C:/Python27/python.exe -c "import sys;print(sys.executable)"', stdout=sub.PIPE).communicate()[0]`. – Eryk Sun Aug 24 '19 at 20:36
  • It makes sense for `Popen` to run the current version if `shell=False` and the call uses an unqualified relative path such as "python.exe". In that case, `CreateProcessW` (as called by `Popen`) always searches the application directory first. But when passed a fully-qualified path, I don't understand why it would run something else. – Eryk Sun Aug 24 '19 at 20:43
  • Thanks for the response. I've edited the question to include the very odd results to your query. – Spacey Sep 09 '19 at 11:46
  • Try a lower-level check: `sub.Popen(r'C:\Python27\python.exe -c "import ctypes; buf=(ctypes.c_wchar * 260)(); ctypes.windll.kernel32.GetModuleFileNameW(None, buf, 260); print(buf.value)"', stdout=sub.PIPE).communicate()[0]`. The result here is without question the real executable of the process. Try it from the command line as well. – Eryk Sun Sep 09 '19 at 18:52

0 Answers0