在python脚本中设置环境变量
我有一个 bash 脚本,用于设置环境变量并运行命令
LD_LIBRARY_PATH=my_path
sqsub -np $1 /homedir/anotherdir/executable
现在我想使用 python 而不是 bash,因为我想计算传递给命令的一些参数。
我已经尝试
putenv("LD_LIBRARY_PATH", "my_path")
并
call("export LD_LIBRARY_PATH=my_path")
遵循,
call("sqsub -np " + var1 + "/homedir/anotherdir/executable")
但程序总是放弃,因为未设置 LD_LIBRARY_PATH 。
我该如何解决这个问题?
感谢您的帮助!
(如果我在调用 python 脚本之前导出 LD_LIBRARY_PATH 一切正常,但我希望 python 确定路径并将环境变量设置为正确的值)
I have a bash script that sets an environment variable an runs a command
LD_LIBRARY_PATH=my_path
sqsub -np $1 /homedir/anotherdir/executable
Now I want to use python instead of bash, because I want to compute some of the arguments that I am passing to the command.
I have tried
putenv("LD_LIBRARY_PATH", "my_path")
and
call("export LD_LIBRARY_PATH=my_path")
followed by
call("sqsub -np " + var1 + "/homedir/anotherdir/executable")
but always the program gives up because LD_LIBRARY_PATH is not set.
How can I fix this?
Thanks for help!
(if I export LD_LIBRARY_PATH before calling the python script everything works, but I would like python to determine the path and set the environment variable to the correct value)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
bash:
类似,在Python中:
bash:
Similar, in Python:
这里有很多好的答案,但您应该不惜一切代价避免使用
shell=True
将不受信任的变量传递给子进程,因为这是一个安全风险。变量可以转义到 shell 并运行任意命令!如果您无法避免它,至少使用 python3 的 shlex.quote() 来转义字符串(如果您有多个空格分隔的参数,请引用每个拆分而不是整个字符串)。shell=False
始终是传递参数数组的默认值。现在安全解决方案...
方法#1
更改您自己的进程环境 - 新环境将应用于python 本身和所有子进程。
方法#2
复制环境和通行证是给孩子的。您可以完全控制子环境,并且不会影响 python 自己的环境。
方法 #3
仅限 Unix:执行
env
设置环境变量。如果你有很多变量需要修改而不是 portabe,那就更麻烦了,但像 #2 一样,你保留对 python 和子环境的完全控制。当然,如果 var1 包含多个空格分隔的参数,它们现在将作为带有空格的单个参数传递。要使用
shell=True
保留原始行为,您必须编写一个包含拆分字符串的命令数组:There are many good answers here but you should avoid at all cost to pass untrusted variables to subprocess using
shell=True
as this is a security risk. The variables can escape to the shell and run arbitrary commands! If you just can't avoid it at least use python3'sshlex.quote()
to escape the string (if you have multiple space-separated arguments, quote each split instead of the full string).shell=False
is always the default where you pass an argument array.Now the safe solutions...
Method #1
Change your own process's environment - the new environment will apply to python itself and all subprocesses.
Method #2
Make a copy of the environment and pass is to the childen. You have total control over the children environment and won't affect python's own environment.
Method #3
Unix only: Execute
env
to set the environment variable. More cumbersome if you have many variables to modify and not portabe, but like #2 you retain full control over python and children environments.Of course if
var1
contain multiple space-separated argument they will now be passed as a single argument with spaces. To retain original behavior withshell=True
you must compose a command array that contain the splitted string:您可以通过使用
shell(使用您的 os.environ)中的子进程来添加元素到您的环境中,方法是使用
You can add elements to your environment by using
and run subprocesses in a shell (that uses your
os.environ
) by using