Django : Can't import 'module'. Check that module AppConfig.name is correct

Question:

Might look like an already answered question, actually here you have the same problem (kind of) i had. My problem is, it’s just a trick, one line, no explanation (and still it’s different but the solution given works, and that’s part of my problem).
Here’s my project structure, simplified:

manage.py
compfactu/---settings.py
          |--__init__.py
          |--core/--------__init__.py
                         |-apps.py  

So here is how I added my app in INSTALLED_APPS:

apps.py

from django.apps import AppConfig


class CoreConfig(AppConfig):
    name = 'core'

settings.py

INSTALLED_APPS = [ 
    ...
    #compfactu modules
    'compfactu.core.apps.CoreConfig',
]

As I read the django 1.11 documentation, and I quote :

New applications should avoid default_app_config. Instead they should require the dotted path to the appropriate AppConfig subclass to be configured explicitly in INSTALLED_APPS.

Well nice, it’s a new application so i should do that : but i’m getting an error. And it’s not a problem of pythonpath, cause i just opened a python shell and I can do from compfactu.core.apps import CoreConfig with no problem (print the sys.path too, everything’s fine).

But I have this error, here’s a full traceback:

Traceback (most recent call last):
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/apps/config.py", line 147, in create
    app_module = import_module(app_name)
  File "/home/jbjaillet/Projets/venvcompfactu/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 'core'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/jbjaillet/Projets/venvcompfactu/lib/python3.5/site-packages/django/apps/config.py", line 151, in create
    app_name, mod_path, cls_name,
django.core.exceptions.ImproperlyConfigured: Cannot import 'core'. Check that 'compfactu.core.apps.CoreConfig.name' is correct.

And from there, all files and class have been generated by django (manage.py startapp).
And when I actually do what’s told in the question I linked above, doing like :

INSTALLED_APPS = [ 
    ...
    #compfactu modules
    'compfactu.core',
]

it works ! And I don’t get that point ! Reading the doc (part i’ve just quoted), it SHOULD NOT work (noting that I don’t have a default_app_config in my __init__.py.

So, as the question where I found the “trick” but no explanation, I’m here asking why it works this way when it shouldn’t, and why the solution in the official doc doesn’t work?

Thank you in advance for you time.

Asked By: Bestasttung

||

Answers:

According to the documentation, AppConfig.name is a full python path to the application.

AppConfig.name

Full Python path to the application, e.g. ‘django.contrib.admin’.

This attribute defines which application the configuration applies to.
It must be set in all AppConfig subclasses.

It must be unique across a Django project.

https://docs.djangoproject.com/en/2.2/ref/applications/#django.apps.AppConfig.name

Try this:

class CoreConfig(AppConfig):
    name = 'compfactu.core'
Answered By: Håken Lid

In your apps.py ensure to include the full path off the app:

class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'compfactu.core'
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.