gpt4 book ai didi

python - Django Wsgi ModuleNotFoundError : No module named 'project_name'

转载 作者:行者123 更新时间:2023-12-04 18:48:35 24 4
gpt4 key购买 nike

我正在尝试使用 WSGI 和 NGINX 部署一个 Django 项目。问题是在启动时,日志显示模块丢失错误
通过 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/..
    ├── 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
因为这可能是什么?如何解决?

最佳答案

根据此答案 https://stackoverflow.com/a/42981216/10905324,您的项目文件夹可能需要添加到 wsgi.py 中的路径

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()

关于python - Django Wsgi ModuleNotFoundError : No module named 'project_name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68834468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com