简体   繁体   English

macOS Sierra 上的 python 操作系统和子进程模块不能使用 DYLD_LIBRARY_PATH 和 LD_LIBRARY_PATH

[英]DYLD_LIBRARY_PATH and LD_LIBRARY_PATH cannot be used by python's os and subprocess modules on macOS Sierra

On macOS Sierra 10.12.6, environment variable LD_LIBRARY_PATH cannot be used by os.system() , subprocess.run() and subprocess.Popen() , even though PATH can be used normally.在MacOS塞拉利昂10.12.6,环境变量LD_LIBRARY_PATH不能被使用的os.system() subprocess.run()subprocess.Popen()即使PATH可以正常使用。 Python version is 3.6.1 . Python 版本是3.6.1 But on Linux(Ubuntu 17.10), this environment variable can also be used rightly.但是在 Linux(Ubuntu 17.10)上,这个环境变量也可以正确使用。 The following python script envv.py can show this problem:以下 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')

On macOS, the output is在 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
$
....

On Linux, the output is在 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:...

Does anyone have any idea?有没有人有任何想法? Thanks a lot!非常感谢!

UPDATE 2018-02-07更新 2018-02-07

Follow @GrahamDumpleton 's suggestion, I tried DYLD_LIBRARY_PATH , but got the same result.按照@GrahamDumpleton 的建议,我尝试了DYLD_LIBRARY_PATH ,但得到了相同的结果。 The test stript is试纸是

#!/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())

And the related output is而相关的输出是

$ python3 envv.py
/opt/intel/compilers_and_libraries_2018.1.126/mac/compiler/lib:...
$
$
...

If you're using subprocess.Popen with shell=True , you can manually propagate the LD_LIBRARY_PATH and DYLD_LIBRARY_PATH variables like so:如果您使用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,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM