Django项目重组:无法导入应用程序

时间:2017-04-25 21:25:43

标签: python django

我正在尝试重构我的项目以匹配最新的两本Django书(Django 1.11)中的建议。

完成重组后,我的应用程序无法导入。

secureDash_project
    ├── README.rst
    ├── config
    │   ├── __init__.py
    │   ├── __pycache__
    │   ├── db.sqlite3
    │   ├── settings
    │   │   ├── __init__.py
    │   │   ├── __pycache__
    │   │   └── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── db.sqlite3
    ├── manage.py
    ├── requirements.txt
    └── secureDash
        ├── __init__.py
        ├── __pycache__
        ├── dash
        │   ├── __init__.py
        │   ├── __pycache__
        │   ├── admin.py
        │   ├── apps.py
        │   ├── forms.py
        │   ├── migrations
        │   │   └── __init__.py
        │   ├── models.py
        │   ├── tests.py
        │   ├── urls.py
        │   └── views.py
        └── templates
            └── dash

settings.py摘录:

...
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(
    os.path.dirname(os.path.abspath(__file__))))
ENV_PATH = os.path.abspath(os.path.dirname(__file__))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Application definition

INSTALLED_APPS = [
    'secureDash.dash.apps.DashConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'django_static_jquery',
    # 'bootstrap3',
] ...

运行时

python3 manage.py runserver --settings=config.settings.settings

我明白了:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x1076b0d08>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.5/site-packages/django/apps/config.py", line 142, in create
    app_module = import_module(app_name)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'dash'

secureDash.dash.apps.DashConfig:

from django.apps import AppConfig


class DashConfig(AppConfig):
    name = 'dash'

1 个答案:

答案 0 :(得分:2)

secureDash.dash.apps.DashConfig中使用secureDash.dash替换INSTALLED_APPS应修复此问题。