Python Django 1.7 应用程序配置导入错误:没有名为 appname.apps 的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25785667/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 1.7 app config ImportError: No module named appname.apps
提问by mennanov
I'm trying to setup a custom application configuration for one of my Django app called 'articles' following the documentation at https://docs.djangoproject.com/en/dev/ref/applications/, but I keep getting ImportError: No module named articles.appswhen execute ./manage.py check(or any other management command such as ./manage.py runserver)
我正在尝试按照https://docs.djangoproject.com/en/dev/ref/applications/ 上的文档为我的一个名为“articles”的 Django 应用程序设置自定义应用程序配置,但我ImportError: No module named articles.apps在执行时一直收到./manage.py check(或任何其他管理命令,例如./manage.py runserver)
This is a tree of the project
这是项目的树
projectname
    ├── apps
    │?? ├── articles
    │?? │?? ├── admin.py
    │?? │?? ├── apps.py
    │?? │?? ├── __init__.py
    │?? │?? ├── migrations
    │?? │?? │?? ├── 0001_initial.py
    │?? │?? │?? └── __init__.py
    │?? │?? ├── models.py
    │?? │?? ├── templates
    │?? │?? │?? └── articles
    │?? │?? ├── templatetags
    │?? │?? │?? ├── articles_tags.py
    │?? │?? │?? └── __init__.py
    │?? │?? ├── tests.py
    │?? │?? ├── urls.py
    │?? │?? └── views.py
    │?? ├── __init__.py
installed app in settings.py:
在settings.py 中安装的应用程序:
INSTALLED_APPS = (
  'grappelli',
  'django.contrib.admin', 
  'django.contrib.auth', 
  'django.contrib.contenttypes', 
  'django.contrib.sessions', 
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'django.contrib.humanize', 
  'grappelli.dashboard', 
  'mptt', 
  'sekizai', 
  'pytils', 
  'sorl.thumbnail',
  'sefaro.apps.utils', 
  'sefaro.apps.seo', 
  'sefaro.apps.staticpages', 
  'sefaro.apps.statictext', 
  'sefaro.apps.usersettings', 
  'sefaro.apps.navigation', 
  'sefaro.apps.slideshow',
  'sefaro.apps.articles', 
) 
Contents of articles/__init__.py:
内容articles/__init__.py:
# articles/__init__.py
default_app_config = 'articles.apps.ArticlesConfig'
Contents of articles/apps.py:
内容articles/apps.py:
# -*- coding: utf-8 -*-
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class ArticlesConfig(AppConfig):
    name = 'articles'
    verbose_name = _(u'Articles')
And I have 'projectname.apps.articles'in my INSTALLED_APPS
我'projectname.apps.articles'在我的INSTALLED_APPS
Just to ensure that I really have all these files and haven't messed up with paths
只是为了确保我真的拥有所有这些文件并且没有弄乱路径
>>> from projectname.apps.articles.apps import ArticlesConfig
>>> ArticlesConfig
<class 'projectname.apps.articles.apps.ArticlesConfig'>
Everything imports just fine...
一切进口都很好......
But:
但:
(vagrant)vagrant@vagrant-ubuntu-trusty-32:~/django$ ./manage.py check
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/vagrant/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/vagrant/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/vagrant/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/vagrant/local/lib/python2.7/site-packages/django/apps/config.py", line 112, in create
    mod = import_module(mod_path)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named articles.apps
采纳答案by mennanov
According to the specific Django project structure (all applications are located in projectname/apps/module) the full pathincluding the project name should be used.
根据特定的 Django 项目结构(所有应用程序都位于projectname/apps/模块中),应使用包括项目名称的完整路径。
As the doc https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.namesays:
正如文档https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.name所说:
AppConfig.name
Full Python path to the application, e.g. 'django.contrib.admin'.
应用配置名称
应用程序的完整 Python 路径,例如“django.contrib.admin”。
So it should be:
所以应该是:
# articles/apps.py:
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class ArticlesConfig(AppConfig):
    name = 'projectname.apps.articles'
    verbose_name = _(u'Articles')
and
和
# articles/__init__.py
default_app_config = 'projectname.apps.articles.apps.ArticlesConfig'
回答by Joanmacat
I think the problem could be focused in your articles/__init__.pyfile.
我认为问题可能集中在您的文章/__init__.py文件中。
I mean... In the documentation says:
我的意思是......在文档中说:
Of course, you can also tell your users to put 'rock_n_roll.apps.RockNRollConfig' in their INSTALLED_APPS setting.
当然,您也可以告诉您的用户将“rock_n_roll.apps.RockNRollConfig”放在他们的 INSTALLED_APPS 设置中。
You tried to delete the "default_app_config" statement and only getting your articles.apps.ArticlesConfigin your INSTALLED_APPS?
您试图删除“default_app_config”语句并且只在您的 INSTALLED_APPS 中获取您的articles.apps.ArticlesConfig?
I say that because the docs says:
我这么说是因为文档说:
That will cause RockNRollConfig to be used when INSTALLED_APPS just contains 'rock_n_roll'.
当 INSTALLED_APPS 只包含 'rock_n_roll' 时,这将导致使用 RockNRollConfig。
In the case to have a default_apps_config declared in articles/__init__.py, in your INSTALLED_APPS it's only necessary:
如果在article/__init__.py 中声明了 default_apps_config ,则在您的 INSTALLED_APPS 中只需要:
INSTALLED_APPS = ( 'grappelli', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'grappelli.dashboard', 'mptt', 'sekizai', 'pytils', 'sorl.thumbnail', 'sefaro.apps.utils', 'sefaro.apps.seo', 'sefaro.apps.staticpages', 'sefaro.apps.statictext', 'sefaro.apps.usersettings', 'sefaro.apps.navigation', 'sefaro.apps.slideshow', 'articles', )
Maybe i'm wrong but I would try that :) Tell me if you need more help.
也许我错了,但我会尝试:) 如果您需要更多帮助,请告诉我。
回答by Krishna G Nair
The name attribute in the app configuration should be same as what we give in the installed apps.
应用配置中的 name 属性应该与我们在已安装应用中给出的相同。
Also default_app_configshould give the correct path to your custom configuration like,  
还default_app_config应该为您的自定义配置提供正确的路径,例如,  
default_app_config = 'projectname.apps.articles.apps.ArticlesConfig

