setting $ENV{variable} when calling python [duplic

2019-08-13 02:49发布

问题:

This question already has an answer here:

  • How to access environment variable values? 9 answers

I'm new to Python and would like reproduce a convenience I used when working in Perl. When calling a Perl script I usually set some $ENV variables (like VERBOSE, DEVELOP and DEBUG). Inside the called script I recover their values using

my $verbose=$ENV{VERBOSE};
my $develop=$ENV{DEVELOP};
my $debug=$ENV{DEBUG};

This allow print stmts conditional on these variables.

Can I do the same thing in Python? I know thanks to previous responses (Thank you!) to use os.environ[var] within the script to access the values. But I have not been able to figure out how to assign a value to a variable when I call the script from the command line as I could when callinbg a Perl script

Can values be set for such variables on the commandline invoking a python script?

TIA

回答1:

They are accessible via the os.environ dictionary.

os.environ['VERBOSE']


标签: python env