问题
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']
来源:https://stackoverflow.com/questions/16316481/setting-envvariable-when-calling-python