I need to assign a different value to a variable if an env variable called 'MYPROJECT_PRODUCTION' is set to True.So ,I created the following code in which I created a module1 file for storing the production values
The main code resides in mycode.py.Here is the directory structure
mypythondir/
mycode.py
module1.py
mycode.py
import os
if __name__=='__main__':
MYNAME='denny'
if os.environ.get('MYPROJECT_PRODUCTION',True):
from module1 import *
print 'myname=',MYNAME
module1.py
MYNAME='damon'
I opened a terminal (in ubuntu) Just to check I ran
mypythondir$echo $MYPROJECT_PRODUCTION
Empty output..So,no variable set yet. Without setting the env variable ,ran mycode.py from mypythondir
mypythondir$python mycode.py
mypythondir$myname= damon
I am confused,why is the value 'damon'.It should be 'denny' since there is no MYPROJECT_PRODUCTION set ,and there is no key 'MYPROJECT_PRODUCTION' in os.environ.
Then I set
mypythondir$export MYPROJECT_PRODUCTION=False
Again I ran the code and even now the result is the same..but this time the key MYPROJECT_PRODUCTION
is in os.environ
What is happening here ? Can someone please help me figure this out?
Your default value is True
. And any non-empty value, including the string "False"
, is true. Change the default value to something false, or omit it entirely.
os.environ is just a mapping object. Did you try simply os.getenv()? See this link: http://docs.python.org/2/library/os.html#process-parameters
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.