python Django WSGI:从命令行运行www.example.com时,没有名为“my_app.settings”的模块wsgi.py

r6hnlfcb  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(120)

我仔细地看过stackoverflow上所有类似的问题,但没有找到有效的解决方案。由于ModuleNotFoundError,我在运行www.example.com文件时遇到问题wsgi.py:没有名为'magic_orb.settings'的模块(我正在创建一个基于AstroGPT预测的魔术球)。我的wsgi.py文件看起来像这样:

import os
import sys

proj_path = ("../magic_orb")
sys.path.append(proj_path)

os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

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

os.chdir(proj_path)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

正如您可能看到的,我已经尝试更改路径并添加get_wsgi_application。此外,我已经将django.setup()添加到manage.py文件中,因为我的主要问题是应用程序加载。我的项目结构如下:

magic_orb
├── db.sqlite3
├── frontpage
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_frontpage.py
│   │   ├── 0002_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── __pycache__
│   ├── tests.py
│   └── views.py
├── magic_orb
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── magic_orb_model
│   ├── pretrained_model.py
│   └── __pycache__
├── manage.py
└── user_data_form
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0002_user_data_form.py
    │   ├── __init__.py
    │   └── __pycache__
    ├── models.py
    ├── __pycache__
    ├── serializers.py
    ├── tests.py
    └── views.py

我不知所措,不知道我还能做什么,也不知道如何重新格式化代码/添加什么。准备提供更多细节。
尝试执行www.example.com时出错wsgi.py:

File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/django_servers/magic_orb/magic_orb/magic_orb/wsgi.py:23
    application = get_wsgi_application()

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/core/wsgi.py:12 in get_wsgi_application
    django.setup(set_prefix=False)

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/__init__.py:19 in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/conf/__init__.py:102 in __getattr__
    self._setup(name)

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/conf/__init__.py:89 in _setup
    self._wrapped = Settings(settings_module)

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/conf/__init__.py:217 in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)

  File /usr/lib/python3.10/importlib/__init__.py:126 in import_module
    return _bootstrap._gcd_import(name[level:], package, level)

  File <frozen importlib._bootstrap>:1050 in _gcd_import

  File <frozen importlib._bootstrap>:1027 in _find_and_load

  File <frozen importlib._bootstrap>:1004 in _find_and_load_unlocked

ModuleNotFoundError: No module named 'magic_orb.settings'

尝试执行frontpage/ www.example.com时出错models.py,导致我修改了wsgi.py

File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/spyder_kernels/py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File ~/django_servers/magic_orb/magic_orb/frontpage/models.py:6
    class Prediction(models.Model):

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/db/models/base.py:129 in __new__
    app_config = apps.get_containing_app_config(module)

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/apps/registry.py:260 in get_containing_app_config
    self.check_apps_ready()

  File ~/django_servers/magic_orb/venv/lib/python3.10/site-packages/django/apps/registry.py:138 in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")

AppRegistryNotReady: Apps aren't loaded yet.

使用绝对路径时,错误是相同的,sys.path如下所示:

print(sys.path)
['/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '', '/home/shbshka/django_servers/magic_orb/venv/lib/python3.10/site-packages', '/home/shbshka/django_servers/magic_orb', '/magic_orb/magic_orb', '/magic_orb/..', '/magic_orb/.', '/magic_orb/', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/../..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/../..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/../..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/../..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/../..', '/home/shbshka/django_servers/magic_orb/magic_orb/magic_orb/..', '../magic_orb/..', '../magic_orb', '../magic_orb', '../magic_orb', '../magic_orb', '../magic_orb', '/home/shbshka/django_servers/magic_orb/magic_orb', '/home/shbshka/django_servers/magic_orb/magic_orb', '/home/shbshka/django_servers/magic_orb/', '/home/shbshka/django_servers/magic_orb/']

UPD:修复了其他文件中的一个小错误,现在一切正常,但从命令行运行www.example.com仍然不行wsgi.py。

那么问题来了-为什么我不能单独运行wsgi.py?

Django 4.2.2
Python 3.10.6

vfh0ocws

vfh0ocws1#

首先,我会尝试将绝对路径添加到sys.path:

sys.path.append("D:/......../magic_orb")              # path to project base dir, not the magic_orb in which you have settings.py 

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

一旦这是工作,您可以尝试修改与路径相对于工作目录等.我还想添加调试:

print("******* sys.path: ", sys.path)

相关问题