gpt4 book ai didi

python - Django 没有名为 'main' 的模块

转载 作者:行者123 更新时间:2023-12-02 02:44:11 25 4
gpt4 key购买 nike

我正在我的电脑上开发一个 Django 项目,然后我创建了一个名为“register”的应用程序,当我尝试运行 manage.py runserver 时,我收到一个奇怪的错误:我使用Python 3.8.3。当我添加注册表单时出现此错误。

  File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Python38\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Python38\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Python38\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python38\lib\site-packages\django\apps\registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "C:\Python38\lib\site-packages\django\apps\config.py", line 116, in create
mod = import_module(mod_path)
File "C:\Python38\lib\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 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'

我的设置.py:

DEBUG = True


ALLOWED_HOSTS = ['*']

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'simple2',
"crispy_forms",
'main.apps.MainConfig',
'register.apps.RegisterConfig',
'register'
]
...


最佳答案

我猜测您没有配置 WSGI 应用程序,因为 Django 的应用程序引擎会在名为 main.py 的文件中查找 app 变量。您可以通过以下两种方式来看待这个问题:

包含您的 WSGI 申请

您仍然可以创建一个名为 main.py 的文件,其中包含 app 变量。您不需要编写任何内容,只需导入它即可(假设您的项目名称是 projekt:

from projekt.wsgi import main as app

添加自定义入口点

Google's Appengine 所示:

entrypoint: Optional. The command that is executed when your app starts. For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify an entrypoint, App Engine will configure and start the Gunicorn webserver.

默认值与您的需要之间的比较:

# default
entrypoint: gunicorn -b :$PORT main:app
# What you need
entrypoint: gunicorn -b :$PORT demosite.wsgi:main

有关 here 的更多信息。 Reference .

关于python - Django 没有名为 'main' 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63072297/

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