Re: environment variable DJANGO_SETTINGS_MODULE is undefined

2,588 views
Skip to first unread message
Message has been deleted

Sieker Adi Jörg

unread,
May 7, 2009, 10:30:08 AM5/7/09
to django...@googlegroups.com
Hi,

On 07.05.2009, at 16:02, bconnors wrote:

>
> I saw this chat related to DJANGO_SETTINGS_MODULE is undefined.
> Hello I am new to Django.
> I am trying to exercise "templates" through the interactive shell,
> however I end up getting this error. Can anyone help?
> here is the exact steps I follow:
> 1) cd C:\Django\mysite
> 2) set DJANGO_SETTINGS_MODULE=mysite.settings I've also tried set
> DJANGO_SETTINGS_MODULE=settings.py
> 3) python
> 4) >>> from django.template import Template
>>>> t = Template("My name is {{ name }}.")
> 5) ERROR: ImportError: Settings cannot be imported, because
> environment variable DJANGO_SETTINGS_MODULE is undefined
>
>

The above is for windows and not unix.

> - Show quoted text -
>
> Setting it to mysite.settings will work if c:\django is on your
> PYTHONPATH.
> Setting it to settings.py won't work at all because of the .py.
> Assuming
> you are running the python from c:\django\mysite, setting
> DJANGO_SETTINGS_MODULE to just 'settings' will work. Or, use 'python
> manage.py shell', which assumes settings.py is in the current
> directory and
> sets the environment up for you.
> Karen
>
>
>
> So I did
>
> 1
> export PYTHONPATH=/usr/lib/python2.5/site-packages
>
>
> 2
> set DJANGO_SETTINGS_MODULE=myapp1.settings
>
>
This should be: export not set.
set is a window command and doesn't or does something different under
*nix.


> 3
> settings.py has :
>
> INSTALLED_APPS = (
> 'django.contrib.auth',
> 'django.contrib.contenttypes',
> 'django.contrib.sessions',
> 'django.contrib.sites',
> 'myapp1.person',
> )
>
>
> when I do
> django-admin.py dumpdata
> Error: Settings cannot be imported, because environment variable
> DJANGO_SETTINGS
> _MODULE is undefined.
> pubuntu@pubuntu:~/django-trunk/build/lib/django/bin/mysite/myapp$
>
>
>
>
>
> on the Django Congratulations screen it has myapp1
> what am I doing wrong?
>
>
>

bconnors

unread,
May 7, 2009, 11:26:43 AM5/7/09
to Django users

pubuntu@pubuntu:~/django-trunk/build/lib/django/bin/myapp1
export DJANGO_SETTINGS_MODULE=myapp1.settings
export PYTHONPATH=/usr/lib/python2.5/site-packages
django-admin.py dumpdata
Error: Could not import settings 'myapp1.settings' (Is it on sys.path?
Does it h
ave syntax errors?): No module named myapp1.settings
pubuntu@pubuntu:~/django-trunk/build/lib/django/bin/myapp1$

my settings.py has

)

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'myapp1.person',
)
pubuntu@pubuntu:~/django-trunk/build/lib/django/bin/myapp1$

I tried doing export DJANGO_SETTINGS_MODULE=myapp1.person as in
settings.py, but got same error.
> > what am I doing wrong?- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -

NoviceSortOf

unread,
May 9, 2009, 7:24:59 AM5/9/09
to Django users

I'm not the world's leading expert -- but today I've
solved the problem inside of my current python
shell using os.environ.keys()

I'll attempt to outline the basics, ">>>" indicates what
i typed on the python shell command line
"$" indicates what echoes back

>>>import os

# now to see what environment keys already exist.
>>>print os.environ.keys()
$ ['LOGNAME', 'USER', 'HOME', 'PATH',...]

# now to see whats in these environ. keys/vars
>>> print os.environ["HOME"] ## for example.
$/home/myhomefolder

# in my case the following was blank raising a nasty key error in
apocalyptic alarming red text ending with
>>>os.environ["DJANGO_SETTINGS_MODULE"]
$..."raise KeyError(key)"

# so had to set it to get moving using
# first i verified i was in my projects path
>>>os.getcwd()
$/home/myfolder/myproject'

then I issued
>>>os.environ["DJANGO_SETTINGS_MODULE"]="settings"

remember to change directories in the shell use something like.
>>>os.chdir("/home/myproject")

i hope that helps, what i'm doing is creating a *.py file I call it
simply called mydevelsets.py for loading this sort of stuff in the
shell before I start, i call it from the command line using
>>>execfile('mydevelsets.py')

i hope that helps.

# from mydevelsets.py
import os

## WTH is the current work directory anyhow
print os.getcwd()

os.chdir("/home/myproject") ## change to project directory
print os.getcwd() ## I'm from Missouri - show me!

# Set the var!
os.environ["DJANGO_SETTINGS_MODULE"]="settings"

## I'm not really from Missouri - but show me anyhow!
print os.environ["DJANGO_SETTINGS_MODULE"]

bconnors

unread,
May 11, 2009, 9:45:49 AM5/11/09
to Django users
thanks for the advice, but

#set DJANGO_SETTINGS_MODULE=mysite.settings
#python manage.py syncdb --settings=settings
# ln -s /usr/lib/python2.5/site-packages/pinax /home/pubuntu/pinax
# python manage.py suncdb
#curl -o pinax-boot.py http://svn.pinaxproject.com/pinax/trunk/scripts/pinax-boo
t.pt
#python pinax-boot.py pinax-env
#pip install -E pinax/env/ django-notification
#source bin/activate
pip install --requirement src/pinax/requirements/external_apps.txt
pip install --requirement src/pinax/requirements/libs.txt
pubuntu@pubuntu:~/comp$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> os.environ["DJANGO_SETTINGS_MODULE]="settings"
File "<stdin>", line 1
os.environ["DJANGO_SETTINGS_MODULE="settings"
^
SyntaxError: invalid syntax
>>> os.getcwd()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>

pubuntu@pubuntu:~/comp$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> os.environ["DJANGO_SETTINGS_MODULE]="settings"
File "<stdin>", line 1
os.environ["DJANGO_SETTINGS_MODULE="settings"
^
SyntaxError: invalid syntax


there must be something I don't have. do i have to install something?

thanks

James Bennett

unread,
May 11, 2009, 4:11:57 PM5/11/09
to django...@googlegroups.com
On Mon, May 11, 2009 at 9:45 AM, bconnors <Bob.C...@wdc.usda.gov> wrote:
> there must be something I don't have. do i have to install something?

You seem to be in need of a basic Python tutorial, one which will
teach you about things like how to import modules before using them
(because, for example, you seem to be hung up on the fact that -- in
Python -- you must type "import os" before trying to use the 'os'
module).


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages