Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...