django - Django 在服务器启动时尝试导入模块失败(“看起来不像模块路径”)
问题描述
似乎我正在按照说明进行操作,但不知何故 Django 并没有将 django_countries 视为应用程序。
错误:
System check identified no issues (0 silenced).
February 18, 2021 - 23:56:01
Django version 3.1.4, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 13, in import_string
module_path, class_name = dotted_path.rsplit('.', 1)
ValueError: not enough values to unpack (expected 2, got 1)
The above exception was the direct cause of the following exception:
(WSGI stuff...)
设置.py:
INSTALLED_APPS = [
'django_countries',
'registration.apps.RegistrationConfig',
.....
项目树:
.
├── db.sqlite3
├── django_project
│ ├── asgi.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── myapp
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── static
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── readme.rst
├── registration
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── static
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── requirements.txt
├── static
│ └── project
├── templates
│ └── project.html
└── venv
├── bin
├── include
├── lib
├── lib64 -> lib
└── pyvenv.cfg
我用 pip 安装了它,使用 virtualenv。我的虚拟机处于活动状态,我可以使用 pip 列表检查它,也可以使用我用来运行 django ( python manage.py runserver
) 的相同解释器导入 django-countries。无论如何,在 django 模型中,我可以清楚地导入和使用 django-countries (pycharm)。
这是怎么回事?
解决方案
我认为我真正应该做的是删除这个问题。但是,一个人必须承认自己的白痴,所以....
显然我最初是这样做的,但从未注意到:
MIDDLEWARE = [
.... middlewares....
'django_countries', ]
我得到了错误,所以认为这是一个 venv 问题并为这个项目重新安装了我的虚拟环境。然后我正确地将其添加到已安装的应用程序中,而不是中间件部分:
INSTALLED_APPS = [
'django_countries',
'registration.apps.RegistrationConfig',
.....
但是由于 Django 导入中间件的过程仍在尝试导入绝对不是中间件的东西,并且该导入的语法错误,因此它在启动时失败并显示以下完整堆栈跟踪:
xception in thread django-main-thread:
Traceback (most recent call last):
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 15, in import_string
module_path, class_name = dotted_path.rsplit('.', 1)
ValueError: not enough values to unpack (expected 2, got 1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
return import_string(app_path)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 19, in import_string
module = import_module(module_path)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/django_project/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
return WSGIHandler()
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/handlers/wsgi.py", line 127, in __init__
self.load_middleware()
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 40, in load_middleware
middleware = import_string(middleware_path)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/utils/module_loading.py", line 17, in import_string
raise ImportError("%s doesn't look like a module path" % dotted_path) from err
ImportError: django_countries doesn't look like a module path
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/usr/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 138, in inner_run
handler = self.get_handler(*args, **options)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
handler = super().get_handler(*args, **options)
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 65, in get_handler
return get_internal_wsgi_application()
File "/run/media/fv/hdd-1/PROJECTS/django-receipes/django_template/venv/lib/python3.9/site-packages/django/core/servers/basehttp.py", line 47, in get_internal_wsgi_application
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: WSGI application 'django_project.wsgi.application' could not be loaded; Error importing module.
推荐阅读
- c# - 系统线程任务字符串到字符串转换错误
- mysql - 大量列,全选需要永远
- pandas - 如何计算数据透视表中的总方差
- c# - 有没有办法在不使用 codesmith 的情况下向 NetTiers 模型添加新列?
- php - 如何使用 mod-rewrite 相应地重定向 API 调用和 Web 应用程序调用
- google-play - 为什么我的应用程序更新在 Google Play 商店中被拒绝,尽管我提交了多次需要的测试登录?
- c# - .Net 反应式扩展:删除重播
- solaris - 框架错误:代码:60 原因:SSL 证书问题:无法在 solaris 11.3 中获取本地颁发者证书
- sorting - 如何在 Hybris 中隐藏特定页面的排序选项
- java - 我的情况无法正常工作我的应用程序在单击按钮时崩溃