gpt4 book ai didi

python - DYLD_LIBRARY_PATH 和 LD_LIBRARY_PATH 不能被 macOS Sierra 上的 python 的 os 和 subprocess 模块使用

转载 作者:行者123 更新时间:2023-12-03 09:52:48 32 4
gpt4 key购买 nike

在 macOS Sierra 10.12.6 上,环境变量 LD_LIBRARY_PATH os.system() 不能使用, subprocess.run()subprocess.Popen() , 即使 PATH可以正常使用。 Python 版本为 3.6.1 .但是在Linux(Ubuntu 17.10)上,这个环境变量也可以正常使用。以下python脚本envv.py可以显示这个问题:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import os
import time

PATH = "PATH"
print(os.environ.get(PATH))
os.system("echo $" + PATH)
subprocess.run("echo $" + PATH, shell=True)
subprocess.run("/bin/echo $" + PATH, shell=True)
subprocess.run("/bin/echo $" + PATH, shell=True, env={PATH: os.environ.get(PATH)})
subprocess.run("/bin/echo $" + PATH, shell=True, env=os.environ)
subprocess.Popen("/bin/echo $" + PATH, shell=True, env=os.environ.copy())
time.sleep(2)
print('\n')

LD_LIBRARY_PATH = "LD_LIBRARY_PATH"
print(os.environ.get(LD_LIBRARY_PATH))
os.system("echo $" + LD_LIBRARY_PATH)
subprocess.run("/bin/echo $" + LD_LIBRARY_PATH, shell=True)
subprocess.run("/bin/echo $" + LD_LIBRARY_PATH, shell=True, env={LD_LIBRARY_PATH: os.environ.get(LD_LIBRARY_PATH)})
subprocess.run("/bin/echo $" + LD_LIBRARY_PATH, shell=True, env=os.environ)
subprocess.Popen("/bin/echo $" + LD_LIBRARY_PATH, shell=True, env=os.environ.copy())
time.sleep(2)
print('\n')

在 macOS 上,输出为
$ python3 envv.py 
/opt/alps/bin:...
/opt/alps/bin:...
/opt/alps/bin:...
/opt/alps/bin:...
/opt/alps/bin:...
/opt/alps/bin:...
/opt/alps/bin:...


/opt/alps/lib:...
$ # use $ to show blank line
$
....

在 Linux 上,输出为
$ python3 envv.py 
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/bin/intel64:...


/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...
/opt/intel/compilers_and_libraries_2018.1.163/linux/compiler/lib/intel64:...

有谁有想法吗?非常感谢!

更新 2018-02-07

按照@GrahamDumpleton 的建议,我试过 DYLD_LIBRARY_PATH ,但得到了相同的结果。试纸是
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import os

DYLD_LIBRARY_PATH = "DYLD_LIBRARY_PATH"
print(os.environ.get(DYLD_LIBRARY_PATH))
os.system("echo $" + DYLD_LIBRARY_PATH)
subprocess.run("/bin/echo $" + DYLD_LIBRARY_PATH, shell=True)
subprocess.run("/bin/echo $" + DYLD_LIBRARY_PATH, shell=True, env={DYLD_LIBRARY_PATH: os.environ.get(DYLD_LIBRARY_PATH)})
subprocess.run("/bin/echo $" + DYLD_LIBRARY_PATH, shell=True, env=os.environ)
subprocess.Popen("/bin/echo $" + DYLD_LIBRARY_PATH, shell=True, env=os.environ.copy())

相关的输出是
$ python3 envv.py
/opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib:...
$
$
...

最佳答案

如果您使用 subprocess.Popenshell=True ,您可以手动传播 LD_LIBRARY_PATHDYLD_LIBRARY_PATH像这样的变量:

import os
import sys
import subprocess

cmd = "/bin/echo $DYLD_LIBRARY_PATH"

sys_env = os.environ.copy()

# We've used os.environ.copy() so we can make mods
# to the subprocess environment if needed without
# affecting the parent process.

if sys.platform == 'darwin':
if "LD_LIBRARY_PATH" in sys_env:
cmd = f"export LD_LIBRARY_PATH={sys_env['LD_LIBRARY_PATH']} && {cmd}"
if "DYLD_LIBRARY_PATH" in sys_env:
cmd = f"export DYLD_LIBRARY_PATH={sys_env['DYLD_LIBRARY_PATH']} && {cmd}"

process = subprocess.Popen(
cmd,
env=sys_env,
shell=True,
)

关于python - DYLD_LIBRARY_PATH 和 LD_LIBRARY_PATH 不能被 macOS Sierra 上的 python 的 os 和 subprocess 模块使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48657710/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com