Why can't environmental variables set in python persist?

前端 未结 7 739
南旧
南旧 2020-11-27 03:55

I was hoping to write a python script to create some appropriate environmental variables by running the script in whatever directory I\'ll be executing some simulation code,

相关标签:
7条回答
  • 2020-11-27 04:39

    What I like to do is use /usr/bin/env in a shell script to "wrap" my command line when I find myself in similar situations:

    #!/bin/bash
    
    /usr/bin/env NAME1="VALUE1" NAME2="VALUE2" ${*}
    

    So let's call this script "myappenv". I put it in my $HOME/bin directory which I have in my $PATH.

    Now I can invoke any command using that environment by simply prepending "myappenv" as such:

    myappenv dosometask -xyz
    

    Other posted solutions work too, but this is my personal preference. One advantage is that the environment is transient, so if I'm working in the shell only the command I invoke is affected by the altered environment.

    Modified version based on new comments

    #!/bin/bash
    
    /usr/bin/env G4WORKDIR=$PWD ${*}
    

    You could wrap this all up in an alias too. I prefer the wrapper script approach since I tend to have other environment prep in there too, which makes it easier for me to maintain.

    0 讨论(0)
提交回复
热议问题