Using Python to open a shell environment, run a command and exit environment

前端 未结 5 1158
感动是毒
感动是毒 2020-12-19 04:39

I\'m trying to automate a process using python. If I am just in the terminal the workflow looks like:

user:> . /path/to/env1.sh
user:> python somethin         


        
5条回答
  •  再見小時候
    2020-12-19 05:11

    subprocess calls (particular Popen) accepts an env argument which is a mapping of environement variables to values. You can use that. e.g.

    env = {'FOO': 'Bar', 'HOME': '/path/to/home'}
    process = subprocess.Popen(['python', 'something.py'], env=env)
    

    Of course, usually, it's better to just call some functions after *import*ing something.py instead of spawning a whole new process.

提交回复
热议问题