英文:
Django WSGI: no module named 'my_app.settings' while running wsgi.py from command line
问题
I've looked carefully through all similar questions on stackoverflow but found no working solution.
我仔细查看了Stack Overflow 上的所有类似问题,但没有找到可行的解决方案。
I have a problem running wsgi.py file due to the ModuleNotFoundError: No module named 'magic_orb.settings' (I'm creating a magic orb with predictions based on AstroGPT). My wsgi.py file looks like this:
我在运行wsgi.py文件时遇到了问题,原因是出现了ModuleNotFoundError: No module named '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()
As you may see, I've already tried changing the path and adding get_wsgi_application. Moreover, I've added django.setup()
to the manage.py file as my main problem is with app loading. My project's structure is the following:
正如您所看到的,我已经尝试更改路径并添加get_wsgi_application。此外,我还在manage.py文件中添加了django.setup()
,因为我的主要问题是应用程序加载。我的项目结构如下:
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
I am at a loss and have no idea what else I can do and how else to reformat the code/what else to add. Ready to provide additional details.
我很茫然,不知道还能做什么,如何重新格式化代码/还需要添加什么。随时提供额外的细节。
Error while trying to execute wsgi.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/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
File
File
ModuleNotFoundError: No module named 'magic_orb.settings'
Error while trying to execute frontpage/models.py, which led me to modify wsgi.py
尝试执行frontpage/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.
With an absolute path, the error is the same and sys.path is the following:
使用绝对路径时,错误仍然相同,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/../..',
英文:
I've looked carefully through all similar questions on stackoverflow but found no working solution.
I have a problem running wsgi.py file due to the ModuleNotFoundError: No module named 'magic_orb.settings' (I'm creating a magic orb with predictions based on AstroGPT). My wsgi.py file looks like this:
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()
As you may see, I've already tried changing the path and adding get_wsgi_application. Moreover, I've added django.setup()
to the manage.py file as my main problem is with app loading. My project's structure is the following:
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
I am at a loss and have no idea what else I can do and how else to reformat the code/what else to add. Ready to provide additional details.
Error while trying to execute 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'
Error while trying to execute frontpage/models.py, which led me to modify 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.
With an absolute path, the error is the same and sys.path is the following:
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/']
Django 4.2.2
Python 3.10.6
答案1
得分: 2
首先,我会尝试将绝对路径添加到sys.path:
sys.path.append("D:/......../magic_orb") # 项目基本目录的路径,而不是magic_orb中的settings.py所在的路径
一旦这个工作正常,你可以尝试使用相对于工作目录的路径进行修改等操作。
我还会添加用于调试的代码:
print("******* sys.path: ", sys.path)
英文:
first I would try to add the absolute path to 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")
Once this is working you can try to modify with path relative to working dir etc.
I would also add for debugging:
print("******* sys.path: ", sys.path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论