Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

python - ImportError: No module named mysite.settings (Django)

I have installed Django and mod_wsgi-express on an ubuntu 15.10. Basically (notice I did not do this as root):

pip install Django
pip install mod_wsgi

Next I did:

~/.local/bin $ ./mod_wsgi-express start-server ~/mysite/mysite/wsgi.py

where: ~/mysite/mysite/wsgi.py comes from the sample project that I uploaded to the above destination on the server. But I get an error when I try to access the website (Internal Server Error). When I look in the log I see:

[Thu Mar 24 22:26:24.638043 2016] [wsgi:error] [pid 19469:tid 139785018738560]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 24 22:26:24.638070 2016] [wsgi:error] [pid 19469:tid 139785018738560]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 24 22:26:24.781030 2016] [wsgi:error] [pid 19469:tid 139785018738560]     __import__(name)
[Thu Mar 24 22:26:24.781148 2016] [wsgi:error] [pid 19469:tid 139785018738560] ImportError: No module named mysite.settings
[Thu Mar 24 22:26:27.590300 2016] [wsgi:error] [pid 19469:tid 139784895194880] [remote 92.243.236.53:24636] mod_wsgi (pid=19469): Target WSGI script '/tmp/mod_wsgi-localhost:8000:1000/handler.wsgi' cannot be loaded as Python module.

So it seems that mysite.settings cannot be found/is not on the Python PATH (the file ~/mysite/mysite/settings.py does exist).

Based on: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/

I tried to add:

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" 

But it did not help. I also tried to add the above path to the sample project to the python path based on: https://code.djangoproject.com/wiki/PythonPath

But same error. What am I missing?

EDIT/Solution:

The problem was in the include path:

import sys
#Wrong!
#sys.path.append("/home/user/mysite/mysite")

#Correct
sys.path.append("/home/user/mysite")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

add this to your wsgi.py file

path = '/home/path/to/project'
if path not in sys.path:
    sys.path.append(path)

before setting

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...