ImportError: no module named mysite.settings (Django)

I installed Django and mod_wsgi-express on ubuntu 15.10. Basically (note that 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 a sample project that I uploaded to the specified destination on the server. But I get an error when trying to access a website (Internal Server Error). When I look in a magazine, 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 in the Python PATH (file ~ / mysite / mysite / settings.py exists).

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 adding the above path to the sample project to the python path based on: https://code.djangoproject.com/wiki/PythonPath

But the same mistake. 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") 
+5
source share
5 answers

The problem was in the include path:

 import sys #Wrong! #sys.path.append("/home/user/mysite/mysite") #Correct sys.path.append("/home/user/mysite") 
+4
source

add this to your wsgi.py file

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

before installation

 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") 
+15
source

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 works for scripts in the main directory of the project.

+6
source

I also had this error when I tried to move a Windows project to Debian.

 import sys sys.path.append("your project path") 
+1
source

In addition, if you are using Visual Studio, make sure your application properties for Django match the expected settings module. By default, it is set to $ (MSBuildProjectName) .settings, which was a problem for me, since my project name and application name were not the same.

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

0
source

Source: https://habr.com/ru/post/1245733/


All Articles