Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't python read my environment variables?

I'm working with Django and I'm trying out to set several environment variables that define which settings are imported.

I use the following folder herarchy:

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

I use Kenneth Reitz's autoenv to load my environment variables. I know it's working correctly because I can type $ 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 python shell and try to print out 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'

Anyone have any clue what could be wrong? Thanks!

Also, I can type $ django-admin.py runserver --settings='project_specific_files.settings.local and everything will work alright 0.o

like image 625
marcoamorales Avatar asked Feb 21 '13 15:02

marcoamorales


1 Answers

Is your environment variable exported?

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

With bash, run: export DJANGO_SETTINGS_MODULE

like image 142
Didier Trosset Avatar answered Nov 15 '22 01:11

Didier Trosset