Loading environment modules within a python script

后端 未结 4 1384
一个人的身影
一个人的身影 2020-12-05 07:13

Is there a way for a python script to load and use environment modules? os.system(\'module load xxx\') doesn\'t work since it executes them in a subshell (at le

4条回答
  •  独厮守ぢ
    2020-12-05 07:46

    Not directly, but here's one possible workaround, depending on your environment. Assuming you can preface your system command with ENVVAR=value, you can do something along these lines:

    import os
    os.environ['EDITOR'] = 'vi'
    cmd = "EDITOR=%(EDITOR)s $EDITOR" % os.environ
    os.system(cmd)
    

    The code assigns vi to your EDITOR environment variable, then passes it on the command line and runs the command, which (in this case) is EDITOR.

提交回复
热议问题