I am doing this on Fedora
Problem:
(sandbox)[root@localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Setup virtualenv
mkdir pythonenv # that's the /home/yeukhon/pythonenv/*.*
cd pythonenv
virtualenv --no-site-packages --distribute sandbox
Install Django
pip install -E sandbox django
# changing mode of /home/yeukhon/pythonenv/sandbox/bin/django-admin.py to 755
# Successfully installed django
Under /home/yeukhon/pythonenv/sandbox
bin include lib mysite
Under lib
You have /lib/python2.7/site-packages/django/*.*
Create Project is fine
(sandbox)[root@localhost sandbox]# django-admin.py startproject mysite
# the path is now /home/yeukhon/pythonenv/sandbox/mysite/*.*
Can't run server
django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
Python Shell under sandbox (following this guide: How to troubleshoot - ImportError: Could not import settings 'mysite.settings' when deploying django?)
(sandbox)[root@localhost mysite]# python
Python 2.7.2 (default, Oct 27 2011, 01:36:46)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import os
>>> os.environ['DJANGO_SETTINGS_MODULE']
'mysite.settings'
>>> os.path.exists('/home')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite')
True
>>> os.path.exists('/home/yeukhon/pythonenv/sandbox/mysite/settings.py')
True
>>> from django.core.management import setup_environ
>>> import mysite.settings
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysite.settings
>>> setup_environ(mysite.settings)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'mysite' is not defined
>>> print sys.path
['',
/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg',
'/home/yeukhon/pythonenv/sandbox/lib/python27.zip',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-tk',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-old',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/home/yeukhon/pythonenv/sandbox/lib/python2.7/site-packages'
What do I need to do to correct this problem? Thank you for your time.
EDIT
Thanks for the response.
I tried the following:
(sandbox)[root@localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/sandbox/"
(sandbox)[root@localhost mysite]# export PYTHONPATH="/home/yeukhon/pythonenv/"
(sandbox)[root@localhost mysite]# deactivate
[root@localhost mysite]# source ../bin/activate
(sandbox)[root@localhost mysite]# django-admin.py runserver
Error: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings
>>> sys.path
['',.... '/home/yeukhon/pythonenv'.....]
It is now on the python path. But I still can't run the server.
Centralized Django Project
Yes. That's a good suggestion. So I suppose that all I need to do is "create a directory called mydjango, then create projects within mydjango". But what commands need to be changed / added? I am willing to learn good practice.
Thank you very much.
Solution (Add to environment variable)
PYTHONPATH=$PYTHONPATH:path-to-your-directory
# PYTHONPATH=$PYTHONPATH:/home/yeukhon/pythonenv/sandbox/
The last line tells you all you need to know. In order to import mysite.settings
, the parent directory of mysite
must be on your PYTHONPATH. It currently isn't.
FWIW, it's not typical to actually store your project in the virtualenv directory. Usually, you put all your projects in on directory that you put on your PYTHONPATH. Then, just load up whatever virtualenv you need, and all's good. In fact, the best part of virtualenv is that they're interchangeable; i.e., you could easily run the same project in multiple different virtualenv environments (such as for testing a new release of Django without altering your normal virtualenv), but there again, you want your projects in one centralized place instead of inside a particular virtualenv directory.
Don't use django-admin.py
for runserver, or indeed for anything other than startproject
. Use manage.py runserver
instead. That sets up all the relevant paths for you, and it should then just work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With