Why can't Python read environment variables?

I am working with Django, and I am trying to set several environment variables that determine which parameters are imported.

I use the following folder hierarchy:

project_folder> app_folder> project_specific_files> settings> base.py local.py 

I am using Kenneth Reitz autoenv to load environment variables. I know that it works correctly, because I can print $ echo $DJANGO_SETTINGS_MODULE , and I see this output:

 project_specific_files.settings.local 

But when I run $django-admin.py runserver , I get:

 ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 

Also, if I go into the python shell and try to print the env variables, I get the following:

 Python 2.7.3 (default, Dec 22 2012, 21:14:12) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> print os.environ['HOME'] /home/marco >>> print os.environ['DJANGO_SETTINGS_MODULE'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/marco/.virtualenvs/myproject/lib/python2.7/UserDict.py", line 23, in __getitem__ raise KeyError(key) KeyError: 'DJANGO_SETTINGS_MODULE' 

Does anyone know what could be wrong? Thanks!

Alternatively, I can print $ django-admin.py runserver --settings='project_specific_files.settings.local and everything will work fine 0.o

+4
source share
1 answer

Is the environment variable exported?

Maybe the environment variable is defined in the current shell, but not marked as exported to subprocesses (python or Django interpreter).

Use bash to run: export DJANGO_SETTINGS_MODULE

+9
source

All Articles