0

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.