14

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
u123
  • 15,603
  • 58
  • 186
  • 303

5 Answers5

19

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")
vvvvv
  • 25,404
  • 19
  • 49
  • 81
northsideknight
  • 1,519
  • 1
  • 15
  • 24
  • 8
    I think this is a better solution as it means the path is not hard coded `path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))` – HenryM May 15 '17 at 06:23
19

Please try:

import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YOURAPP.settings")

This is working for scripts in main project directory, as well.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
porto
  • 555
  • 4
  • 7
  • very helpful sir, it worked in my django wagtail project. It was very annoying that there were no nginx error logs at all to help debug "Internal server error" – paul100 Aug 01 '17 at 09:11
  • After hours looking for a solution and trying several tips, it is finally SOLVED!!! The problem occurred with Ubuntu 20.04 with Django 3 and Python 3.8. Thank you! – Antonio José May 03 '20 at 02:32
  • Thanks! I had the same issue and adding this code solved it :) . – Aldranor Mar 02 '22 at 14:52
6

The problem was in the include path:

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

#Correct
sys.path.append("/home/user/mysite")
u123
  • 15,603
  • 58
  • 186
  • 303
1

I had this error too, when I was trying to move a windows project to Debian.

import sys
sys.path.append("your project path")
gofr1
  • 15,741
  • 11
  • 42
  • 52
0

Also, if you are using Visual Studio, check that your app properties for Django match the settings module that you are expecting. By default, it is set to $(MSBuildProjectName).settings, which was a problem for me since my project name and app name were not the same.

You can find this setting by right clicking on your app in the Solution Explorer and clicking on "Properties" and then the Django tab on the left.

Dima
  • 655
  • 2
  • 8
  • 20