Gabriel NV Ionescu Gabriel NV Ionescu - 3 months ago 9
Python Question

Dotted path in Django's INTALLED_APPS fails to import

I have a django project of the canonical form:


.
├── app1/
├── app2/
├── ...
├── project/
└── manage.py


With all the additional folders that have started to add up that are not apps (e.g. docs) it's getting harder to keep it organized, so I have decided to make like so:


.
├── modules/
│ ├── __init__.py
│   ├── app1/
│   └── app2/
├── project/
└── manage.py


However, I cannot, for the life of me, figure out how to reference the apps in my settings.py INSTALLED_APPS. Changing


INSTALLED_APPS = [
...
'app1.apps.App1Config' --> 'modules.app1.apps.App1Config'
'app2.apps.App2Config' --> 'modules.app2.apps.App1Config'
]


will yield the cryptic traceback on ./manage.py runserver (/path/to/venv/ for brevity)


Traceback (most recent call last):
File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/path/to/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/path/to/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/path/to/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/path/to/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/path/to/venv/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/path/to/venv/lib/python3.5/site-packages/django/apps/config.py", line 142, in create
app_module = import_module(app_name)
File "/path/to/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 986, in _gcd_import
File "", line 969, in _find_and_load
File "", line 956, in _find_and_load_unlocked
ImportError: No module named 'app1'


Strangely enough, if I remove them from INSTALLED_APPS, the project will (barely) work. For example, requests are funneled to their respective urls.py, views are rendered, etc.

I have tried creating an empty new project with a new clean virtual environment with only Django 1.10 running on Python 3.5 but I cannot figure it out. (yes, I have
__init__.py
wherever neccesary)

What am I missing?

Answer

As well as updating INSTALLED_APPS, you need to update your AppConfig.name attributes to use the new path. For example:

class App1Config(AppConfig):
    name = 'modules.app1'
    ...
Comments