6

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/

2 Answers 2

6

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.

3
  • Pratt. Thank you very much for your time. I just added the path to sys.path. See my edit. Maybe I did the adding incorrectly? NO luck. Thank you though.
    – CppLearner
    Dec 29, 2011 at 21:11
  • 1
    You can't just add it to sys.path. That's created a runtime. It needs to be in your PYTHONPATH environment variable. Dec 29, 2011 at 21:21
  • You are right. I had to PYTHONPATH=$PYTHONPATH:/home/yeukhon/pythonenv/sandbox/. Thanks.
    – CppLearner
    Dec 29, 2011 at 22:11
3

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.

3
  • (sandbox)[root@localhost mysite]# manage.py runserver bash: manage.py: command not found... That's the problem that I am also expercing.
    – CppLearner
    Dec 29, 2011 at 22:07
  • I did what Chris Pratt. Now both commands are working (with python in the front of course)
    – CppLearner
    Dec 29, 2011 at 22:12
  • +1 didn't even notice that he was using django-admin.py instead of manage.py. Dec 29, 2011 at 22:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.