Django项目重组:无法导入应用程序
Posted
技术标签:
【中文标题】Django项目重组:无法导入应用程序【英文标题】:Django project restructure: Can't import app 【发布时间】:2017-09-23 02:13:49 【问题描述】:我正在尝试重组我的项目,以匹配最新的两勺 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'
【问题讨论】:
你试过用secureDash.dash
替换secureDash.dash.apps.DashConfig
吗?
不。不过那行得通。你能帮我理解为什么会这样吗?
django
查找您的 apps
根目录。所以在这种情况下,它是secureDash/dash/
文件夹。之后它会寻找views.py
和urls.py
来配置它。
@kiran.koduru,这是正确的答案并且描述得很好。请您将其添加为答案吗?
@EminMastizada 完成!
【参考方案1】:
在INSTALLED_APPS
中将secureDash.dash.apps.DashConfig
替换为secureDash.dash
应该可以解决此问题。
【讨论】:
***.com/a/46178088/5180047对此有更好的回答以上是关于Django项目重组:无法导入应用程序的主要内容,如果未能解决你的问题,请参考以下文章
将 django cms 作为应用程序导入现有的 django 项目
Django 无法从应用程序文件夹导入应用程序,ImportError