11

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.apps when execute ./manage.py check (or any other management command such as ./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:

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
default_app_config = 'articles.apps.ArticlesConfig'

Contents of 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

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
Dhia
  • 10,119
  • 11
  • 58
  • 69
mennanov
  • 1,195
  • 3
  • 16
  • 27
  • Can you show your INSTALLED_APPS setting? – Daniel Roseman Sep 11 '14 at 11:01
  • 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', ) – mennanov Sep 11 '14 at 11:09

3 Answers3

16

According to the specific Django project structure (all applications are located in projectname/apps/ module) the full path including the project name should be used.

As the doc https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.name says:

AppConfig.name

Full Python path to the application, e.g. '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'
Community
  • 1
  • 1
mennanov
  • 1,195
  • 3
  • 16
  • 27
1

The name attribute in the app configuration should be same as what we give in the installed apps.

Also default_app_config should give the correct path to your custom configuration like,

default_app_config = 'projectname.apps.articles.apps.ArticlesConfig
Spidy
  • 1,137
  • 3
  • 28
  • 48
Krishna G Nair
  • 268
  • 2
  • 3
0

I think the problem could be focused in your articles/__init__.py file.

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.

You tried to delete the "default_app_config" statement and only getting your articles.apps.ArticlesConfig in your INSTALLED_APPS?

I say that because the docs says:

That will cause RockNRollConfig to be used when INSTALLED_APPS just contains 'rock_n_roll'.

In the case to have a default_apps_config declared in articles/__init__.py, in your INSTALLED_APPS it's only necessary:

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.

Joanmacat
  • 1,442
  • 1
  • 10
  • 13
  • I tried to put `'articles'`, `'sefaro.articles.apps.ArticlesConfig'` in my INSTALLED_APPS, but no luck :( The only correct variant which actually works is 'sefaro.apps.articles', but only if to comment out the `default_app_config` in `articles/__init__.py` (so no custom configuration is enabled) I'll try to reload my vagrant box and keep my fingers crossed... – mennanov Sep 11 '14 at 15:13
  • Reloading my Vagrant box did not help – mennanov Sep 11 '14 at 15:34
  • Seems you solved your problem then :) I learned too how to fix that kind of issues. Thanks very much! – Joanmacat Sep 11 '14 at 18:59