Running Python 3 scripts in Houdini as a Subprocess

   2860   3   2
User Avatar
Member
1 posts
Joined: Dec. 2016
Offline
Hi,

I am trying to run my code in Python 3 from within Houdini using the subprocess module. The script is using PySide2.

Here is my simplified sample script:

import subprocess
import sys
import os

pythonhome = 'C:/python36/'
env = os.environ
env['PYTHONPATH'] = 'C:\\Python36'
env['PYTHONHOME'] = 'C:\\Python36\\bin'
env['PATH'] = env['PATH'].replace('C:/PROGRA~1/SIDEEF~1/HOUDIN~1.258/python27;', '')
print(env['PATH'])

testProc = subprocess.Popen([pythonhome + 'python.exe', r'C:\path\to\python\script.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, shell=True)

while True:
    line = testProc.stdout.readline()
    print(line)
    if not line:
        break

I have tried various combinations of environments and also attempted to run this with a QRunnable and every time I have attempted it, I get some variation of this error:

Fatal Python error: Py_Initialize: unable to load the file system codec

ModuleNotFoundError: No module named ‘encodings’
OR
CodecRegistryError.

Let me know if anyone has any insights on this.

Thanks
User Avatar
Member
2 posts
Joined: June 2017
Offline
Try to add -E argument when starting python3 from python2.

# According to help of python
# -E     : ignore PYTHON* environment variables (such as PYTHONPATH)
subprocess.Popen( ['C:/Python37/python.exe', '-E', 'some_script.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True )
Edited by bigbalabong - Oct. 13, 2021 02:02:05
User Avatar
Member
359 posts
Joined: April 2017
Offline
Is PySide2 included as part of your system environment? You get it "for free" inside Houdini, but if your external script is importing PySide2 then all dependent libraries need to be available at the system level.
MOPs (Motion Operators for Houdini): http://www.motionoperators.com [www.motionoperators.com]
User Avatar
Member
51 posts
Joined: July 2005
Offline
bigbalabong, thank you! -E worked here for subprocess
  • Quick Links