Django Wsgi ModuleNotFoundError: Нет модуля с именем 'project_name'
Я пытаюсь развернуть проект Django, используя WSGI и NGINX. Проблема в том, что при запуске в логах появляется ошибка отсутствия модуля
При запуске с помощью команды service uwsgi restart
в журналах /var/log/uwsgi/app/django.log появляется ошибка:
Wed Aug 18 17:25:21 2021 - Python version: 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0]
Wed Aug 18 17:25:21 2021 - *** Python threads support is disabled. You can enable it with --enable-threads ***
Wed Aug 18 17:25:21 2021 - Python main interpreter initialized at 0x55f90e1e7700
Wed Aug 18 17:25:21 2021 - your server socket listen backlog is limited to 100 connections
Wed Aug 18 17:25:21 2021 - your mercy for graceful operations on workers is 60 seconds
Wed Aug 18 17:25:21 2021 - mapped 145840 bytes (142 KB) for 1 cores
Wed Aug 18 17:25:21 2021 - *** Operational MODE: single process ***
Traceback (most recent call last):
File "project_name/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/usr/local/lib/python3.8/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 82, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.8/dist-packages/django/conf/__init__.py", line 170, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'project_name'
Wed Aug 18 17:25:22 2021 - unable to load app 0 (mountpoint='') (callable not found or import error)
Wed Aug 18 17:25:22 2021 - *** no app loaded. going in full dynamic mode ***
Wed Aug 18 17:25:22 2021 - *** uWSGI is running in multiple interpreter mode ***
Wed Aug 18 17:25:22 2021 - spawned uWSGI master process (pid: 223091)
Wed Aug 18 17:25:22 2021 - spawned uWSGI worker 1 (pid: 223102, cores: 1)
wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.production')
application = get_wsgi_application()
/etc/uwsgi/apps-enabled/django.ini:
[uwsgi]
chdir = /root/project_name
env = DJANGO_SETTINGS_MODULE=project_name.settings.production
wsgi-file = project_name/wsgi.py
workers = 1
plugins = python3
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.production')
application = get_wsgi_application()
. структура /root/имя_проекта/..
├── project_name
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings
│ │ ├── base.py
│ │ ├── development.py
│ │ ├── __init__.py
│ │ ├── production.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── web_site
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── models.py
│ └── views.py
Почему это может быть? Как это исправить?
Вашим папкам проекта может потребоваться добавить путь в wsgi.py, согласно этому ответу https://stackoverflow.com/a/42981216/10905324
import os
import sys
sys.path.append('/home/django_projects/MyProject')
sys.path.append('/home/django_projects/MyProject/project_name')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings.production')
application = get_wsgi_application()