Mayapy Unable to Spawn to System Python Environment

Mayapy Unable to Spawn to System Python Environment

robotProdigy
Advocate Advocate
1,820 Views
1 Reply
Message 1 of 2

Mayapy Unable to Spawn to System Python Environment

robotProdigy
Advocate
Advocate

Hello, 

I'm having an issue spawning a system Python 2.7 script from the mayapy Python environment. Here is a small example of what I can't get to work. aaa.py uses subprocess to spawn bbb.py. 

aaa.py

import subprocess

python_path = r'c:\python27\python.exe'
callee_path = r'c:\temp\bbb.py'

proc_obj = subprocess.Popen([python_path, callee_path],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)

out, err = proc_obj.communicate()
print('out -> %s' % out.decode())
print('err -> %s' % err.decode())

bbb.py

print('Hello from bbb.py')


If I run the contents of aaa.py inside a Maya script editor, all is well. 

If I run this aaa.py using Python 2.7 as the Python env from the shell like this, I get what is expected - "Hello from bbb.py":

C:\>"C:\python27\python.exe" "c:\temp\aaa.py"
out -> Hello from bbb.py
err ->


However, if I do the same thing, except using mayapy Python environment instead, I get this:

C:\>"C:\Program Files\Autodesk\Maya2016\bin\mayapy.exe" "c:\temp\aaa.py"
out ->
err -> ImportError: No module named site

I've beat my head, and googled this to death - and then sum. Does anyone have any ideas of what is going on or how to get this to work? 

This is an issue on Windows 7, Windows 10, Maya 2016, Maya 2017. 


Thanks for reading and cheers,
Paul

0 Likes
Accepted solutions (1)
1,821 Views
1 Reply
Reply (1)
Message 2 of 2

robotProdigy
Advocate
Advocate
Accepted solution

OK, 

 

I found a solution, or work-around - I'm not sure which. Simply setting the PYTHONHOME environment variable to my Python 2.7 exe directory within aaa.py fixes the issue, despite the fact that aaa.py is to be run inside a mayapy environment. It doesn't make sense...yet. So the aaa.py now looks like this.

 

aaa.py

import subprocess

import os
os.environ['PYTHONHOME'] = r'C:\Python27'

python_path = r'c:\python27\python.exe'
callee_path = r'c:\temp\bbb.py'

proc_obj = subprocess.Popen([python_path, callee_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

out, err = proc_obj.communicate()
print('out -> %s' % out.decode())
print('err -> %s' % err.decode())

now when I run, I get this - correct feedback with no error:

C:\Users\pixel>"C:\Program Files\Autodesk\Maya2016\bin\mayapy.exe" "c:\temp\aaa.py"
out -> Hello from bbb.py
err ->
0 Likes