Set shell environment variable via python script

后端 未结 3 1647
囚心锁ツ
囚心锁ツ 2020-12-06 11:40

I have some instrument which requires environment variable which I want to set automatically from python code. So I tried several ways to make it happen, but none of them we

3条回答
  •  难免孤独
    2020-12-06 12:24

    Depending on how you execute your instrument, you might be able to change environment specifically for the child process without affecting the parent. See documentation for os.spawn*e or subprocess.Popen which accept separate argument denoting child environment. For example, Replacing the os.spawn family in subprocess module documentation which provides both usages:

    Environment example:

    os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
    ==>
    Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
    

提交回复
热议问题