0

我被困在我眼中的一个非常奇怪的行为上。我想从 Houdini ( https://www.sidefx.com/ ) 创建一个进程,这个进程应该初始化 conda 并激活一个环境。如果我使用 python 2.7 从终端运行我的代码,则一切正常。

import os
import sys
import subprocess
conda_path = "/home/alelidis/miniconda3"

print("============================ Python version ============================")
print (sys.version)

my_env = {}
my_env["PYTHONHOME"] = conda_path + ":" + conda_path + "/envs/tflow1.15.0-cuda10-python3"
my_env["PATH"] = '/home/alelidis/miniconda3/bin:/opt/hfs18.0.566/bin:/opt/hfs18.0.566/houdini/sbin:/usr/bin'

script_cmd = 'source ' + os.path.join(conda_path, 'etc/profile.d/conda.sh')  + ' && ' 
script_cmd += 'conda activate tflow1.15.0-cuda10-python3' + ' && '
script_cmd += 'python --version' + ' && '
script_cmd += 'conda deactivate'


#print script_cmd
print("---------------------")
print ("PATH:")
# If shell=True, the command string is interpreted as a raw shell command.
print subprocess.check_output('echo $PATH', shell=True, env=my_env, executable='/bin/bash')
print ("PYTHONHOME:")
print subprocess.check_output('echo $PYTHONHOME', shell=True, env=my_env, executable='/bin/bash')
print ("python version:")
print subprocess.check_output('python --version', shell=True, env=my_env, executable='/bin/bash')
print ("python should be: 3.8.3")

print("++++++++++++++++++++++++++")
print subprocess.check_output(script_cmd, shell=True, executable='/bin/bash')

返回以下输出:

============================ Python version ============================
2.7.15 |Anaconda, Inc.| (default, Dec 14 2018, 19:04:19) 
[GCC 7.3.0]
---------------------
PATH:
/home/alelidis/miniconda3/bin:/opt/hfs18.0.566/bin:/opt/hfs18.0.566/houdini/sbin:/usr/bin

PYTHONHOME:
/home/alelidis/miniconda3:/home/alelidis/miniconda3/envs/tflow1.15.0-cuda10-python3

python version:
Python 3.8.3

python should be: 3.8.3
++++++++++++++++++++++++++
source /home/alelidis/miniconda3/etc/profile.d/conda.sh && conda activate tflow1.15.0-cuda10-python3 && python --version && conda deactivate
++++++++++++++++++++++++++
Python 3.7.9

但是,当我尝试从 Houdini 运行相同的代码时,出现以下错误:

============================ Python version ============================
2.7.15 (default, Mar 11 2020, 10:04:04) 
[GCC 6.3.1 20170216 (Red Hat 6.3.1-3)]
---------------------
PATH:
/home/alelidis/miniconda3/bin:/opt/hfs18.0.566/bin:/opt/hfs18.0.566/houdini/sbin:/usr/bin

PYTHONHOME:
/home/alelidis/miniconda3/bin:/home/alelidis/miniconda3/envs/tflow1.15.0-cuda10-python3

python version:
Python 3.8.3

python should be: 3.8.3
++++++++++++++++++++++++++
source /home/alelidis/miniconda3/etc/profile.d/conda.sh && conda activate tflow1.15.0-cuda10-python3 && python --version && conda deactivate
++++++++++++++++++++++++++
Traceback (most recent call last):
  File "/home/alelidis/miniconda3/bin/conda", line 12, in <module>
    from conda.cli import main
  File "/home/alelidis/miniconda3/lib/python3.8/site-packages/conda/__init__.py", line 22, in <module>
    from .common.compat import text_type, iteritems
  File "/home/alelidis/miniconda3/lib/python3.8/site-packages/conda/common/compat.py", line 14, in <module>
    from tempfile import mkdtemp
  File "/home/alelidis/miniconda3/lib/python3.8/tempfile.py", line 45, in <module>
    from random import Random as _Random
  File "/home/alelidis/miniconda3/lib/python3.8/random.py", line 41, in <module>
    from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ModuleNotFoundError: No module named 'math'

我不知道为什么会这样!任何帮助或指针将不胜感激!

4

2 回答 2

0

This could still be an environment issue. Try inheriting the current environment then editing it instead of creating a blank one. You're also completely overriding the PATH env var instead of adding/editing an entry which could cause issues. Aonther thing to try is setting working directory for the subprocess command. But still not sure why you're getting that error.

my_env = os.environ.copy()
my_env["PYTHONHOME"] = conda_path + ":" + conda_path + "/envs/tflow1.15.0-cuda10-python3"
my_env["PATH"] = '/home/alelidis/miniconda3/bin:/opt/hfs18.0.566/bin:/opt/hfs18.0.566/houdini/sbin:/usr/bin'

You could also try executing a python script instead of using standard I/O. I haven't tried Anaconda but I've successfully launched MayaPy from Houdini like this:

cmdArgs = [mayaPyPath, pyScriptPath]
output = subprocess.call(cmdArgs, cwd=mayaPyWorkDir, shell=False, env=curEnv)
于 2020-09-21T20:40:02.897 回答
0

我发现 Houdini 正在覆盖我使用 python 设置的环境变量。因此,在我通过内部 GUI 删除 PYTHONHOME 变量后,它按预期工作。老实说,我仍然不完全确定为什么会这样。

于 2020-09-22T13:55:51.653 回答