Django的INSTALLED_APPS中的虚线路径无法导入[重复]

这个问题在这里已有答案:

我有一个规范形式的django项目:

.
├── app1/
├── app2/
├── ...
├── project/
└── manage.py

由于已经开始添加的所有其他文件夹都不是应用程序(例如文档),因此保持组织起来变得越来越困难,所以我决定这样做:

.
├── modules/
│   ├── __init__.py
│   ├── app1/
│   └── app2/
├── project/
└── manage.py

但是,对于我的生活,我不能弄清楚如何在我的settings.py INSTALLED_APPS中引用应用程序 . 更改

INSTALLED_APPS = [
    ...
    'app1.apps.App1Config'   -->   'modules.app1.apps.App1Config'
    'app2.apps.App2Config'   -->   'modules.app2.apps.App1Config'
]

将在./manage.py runserver上产生神秘的回溯(/ path / to / venv /为简洁起见)

Traceback (most recent call last):
      File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/path/to/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
        autoreload.raise_last_exception()
      File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
        six.reraise(*_exception)
      File "/path/to/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
        fn(*args, **kwargs)
      File "/path/to/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/path/to/venv/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
        app_config = AppConfig.create(entry)
      File "/path/to/venv/lib/python3.5/site-packages/django/apps/config.py", line 142, in create
        app_module = import_module(app_name)
      File "/path/to/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "", line 986, in _gcd_import
      File "", line 969, in _find_and_load
      File "", line 956, in _find_and_load_unlocked
    ImportError: No module named 'app1'

奇怪的是,如果我从INSTALLED_APPS中删除它们,项目将(几乎没有)起作用 . 例如,请求被汇集到各自的urls.py,呈现视图等 .

我尝试用一个新的干净的虚拟环境创建一个空的新项目,只有Django 1.10在Python 3.5上运行,但我无法理解 . (是的,我有 __init__.py 无论何处需要)

我错过了什么?

回答(1)

2 years ago

除了更新 INSTALLED_APPS 之外,您还需要更新 AppConfig.name 属性以使用新路径 . 例如:

class App1Config(AppConfig):
    name = 'modules.app1'
    ...