在python脚本中设置环境变量

时间:2022-06-01 19:14:24

I have a bash script that sets an environment variable an runs a command

我有一个bash脚本,用于设置环境变量并运行命令

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.

现在我想使用python而不是bash,因为我想计算一些我传递给命令的参数。

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.

但是程序总是放弃,因为没有设置LD_LIBRARY_PATH。

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)

(如果我在调用python脚本之前导出LD_LIBRARY_PATH一切正常,但我希望python确定路径并将环境变量设置为正确的值)

3 个解决方案

#1


35  

bash:

庆典:

LD_LIBRARY_PATH=my_path
sqsub -np $1 /path/to/executable

Similar, in Python:

类似,在Python中:

import os
import subprocess
import sys

os.environ['LD_LIBRARY_PATH'] = "my_path" # visible in this process + all children
subprocess.check_call(['sqsub', '-np', sys.argv[1], '/path/to/executable'],
                      env=dict(os.environ, SQSUB_VAR="visible in this subprocess"))

#2


8  

You can add elements to your environment by using

您可以使用添加元素到您的环境

os.environ['LD_LIBRARY_PATH'] = 'my_path'

and run subprocesses in a shell (that uses your os.environ) by using

并使用在shell(使用您的os.environ)中运行子进程

subprocess.call('sqsub -np ' + var1 + '/homedir/anotherdir/executable', shell=True)

#3


-1  

Compact solution (provided you don't need other environment variables):

紧凑型解决方案(假设您不需要其他环境变量):

call('sqsub -np {} /homedir/anotherdir/executable'.format(var1).split(),
      env=dict(LD_LIBRARY_PATH=my_path))

Using the env command line tool:

使用env命令行工具:

call('env LD_LIBRARY_PATH=my_path sqsub -np {} /homedir/anotherdir/executable'.format(var1).split())

#1


35  

bash:

庆典:

LD_LIBRARY_PATH=my_path
sqsub -np $1 /path/to/executable

Similar, in Python:

类似,在Python中:

import os
import subprocess
import sys

os.environ['LD_LIBRARY_PATH'] = "my_path" # visible in this process + all children
subprocess.check_call(['sqsub', '-np', sys.argv[1], '/path/to/executable'],
                      env=dict(os.environ, SQSUB_VAR="visible in this subprocess"))

#2


8  

You can add elements to your environment by using

您可以使用添加元素到您的环境

os.environ['LD_LIBRARY_PATH'] = 'my_path'

and run subprocesses in a shell (that uses your os.environ) by using

并使用在shell(使用您的os.environ)中运行子进程

subprocess.call('sqsub -np ' + var1 + '/homedir/anotherdir/executable', shell=True)

#3


-1  

Compact solution (provided you don't need other environment variables):

紧凑型解决方案(假设您不需要其他环境变量):

call('sqsub -np {} /homedir/anotherdir/executable'.format(var1).split(),
      env=dict(LD_LIBRARY_PATH=my_path))

Using the env command line tool:

使用env命令行工具:

call('env LD_LIBRARY_PATH=my_path sqsub -np {} /homedir/anotherdir/executable'.format(var1).split())