[英]How to specify LD_LIBRARY_PATH using subprocess in Python3?
I am trying to specify LD_LIBRARY_PATH before the executable name while using subprocess in python3 but getting an error specified next.在 python3 中使用子进程时,我试图在可执行文件名称之前指定 LD_LIBRARY_PATH,但接下来出现指定的错误。
Can you explain what is wrong?你能解释一下哪里出了问题吗? I just want to pass LD_LIBRARY_PATH before the exe name having multiple key-value pair arguments as shown.我只想在具有多个键值对 arguments 的 exe 名称之前传递 LD_LIBRARY_PATH,如图所示。
myFile = "/home/user1/file1.txt"
output_path="/usr/local/dir1/dir2/bin/myExe"
if os.name != 'nt':
libpath = "LD_LIBRARY_PATH=" + os.path.dirname(output_path)
output_path = libpath + " " + output_path;
print("\n Output path is '{}'".format(output_path))
opt1 = ['--input', myFile]
child = subprocess.Popen([output_path, "--header", *opt1,
'--opt2', 'one' '--opt2', 'two'],
shell=False, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
# output bytes
output = child.communicate()[0]
# exit code
rc = child.returncode
Error given is as below给出的错误如下
FileNotFoundError: [Errno 2] No such file or directory: 'LD_LIBRARY_PATH=/usr/local/dir1/dir2/bin /usr/local/dir1/dir2/bin/myExe': 'LD_LIBRARY_PATH=/usr/local/dir1/dir2/bin /usr/local/dir1/dir2/bin/myExe'
You are attempting to use the POSIX shell syntax for specifying environment variables when invoking a command, but you are explicitly telling Python not to invoke a shell (which is good, since using shell=True
leads to non-portable code, and is unnecessary here).您正在尝试使用 POSIX shell 语法在调用命令时指定环境变量,但您明确告诉 Python不要调用 shell(这很好,因为使用shell=True
会导致不可移植的代码,并且在这里是不必要的). Incidentally, shell=False
is the default so you don't need to specify it.顺便说一下, shell=False
是默认设置,因此您无需指定它。
To specify environment variables for Popen
, pass them as a mapping to the env
parameter.要为Popen
指定环境变量,请将它们作为映射传递给env
参数。 Be sure to include all environment variables though, not just the ones you want to change:请务必包括所有环境变量,而不仅仅是您要更改的环境变量:
output_path = '/usr/local/dir1/dir2/bin/myExe'
env = os.environ.copy()
env.update(LD_LIBRARY_PATH = os.path.dirname(output_path))
child = subprocess.Popen(
[output_path, '--header', *opt1, '--opt2', 'one' '--opt2', 'two'],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.