我被困在我眼中的一个非常奇怪的行为上。我想从 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'
我不知道为什么会这样!任何帮助或指针将不胜感激!