Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33378 closed Bug (duplicate)

ImproperlyConfigured error is raised when installing apps in subdirectory

Reported by: Zack West Owned by: nobody
Component: Core (Management commands) Version: 4.0
Severity: Normal Keywords: Apps, Subdirectory, Installation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

4.0 Throws an exception when installing apps that are located in subdirectories.

Example, for a project of such structure:

.
└── DjangoProject/
    └── Apps/
        ├── bestapp/
        │   ├── migrations
        │   ├── __init__.py
        │   ├── admin.py
        │   ├── apps.py
        │   ├── models.py
        │   ├── tests.py
        │   └── views.py
        └── ...

When adding this line to DjangoProject/settings.py:

INSTALLED_APPS = [
    ...
    'Apps.bestapp.apps.BestappConfig'
]

The following exception is thrown when running python manage.py makemigrations:

django.core.exceptions.ImproperlyConfigured: Cannot import 'bestapp'. Check that 'Apps.bestapp.apps.BestappConfig.name' is correct.

The current workaround is to go into the Apps/bestapp/apps.py file and make the following change:

Auto-generated version:

class BestappConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'bestapp' 

Manually-Updated version:

class BestappConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'Apps.bestapp' 

Note the change being adding the parent directory here: name = 'Apps.bestapp'

IIRC, there was a syntax change for app installation somewhere in 3.x. Not sure if this is a product of that change or something else.

Change History (2)

comment:1 by Mariusz Felisiak, 3 years ago

Component: Core (Other)Core (Management commands)
Resolution: duplicate
Status: newclosed
Summary: django.core.exceptions.ImproperlyConfigured Error when installing apps in subdirectoryImproperlyConfigured error is raised when installing apps in subdirectory

Thanks for the report, however it works exactly the same in Django 3.2, 3.1, etc. I don't see any change in this behavior. AppConfig.name must be a full Python path to the application, as documented, so in your case Apps.bestapp is required and expected. We tried to improve generated apps.py files in #30618 but it was clunky.

Duplicate of #30618.

in reply to:  1 comment:2 by Zack West, 3 years ago

Thanks and sorry for the duplicate post. It wasn't so much the requirement of the full path I noted as much as it was Django *generating* a non-fullpath when running the python manage.py startapp command or, more particularly, if one is in a subdirectory (e.g. apps/) and runs the command django-admin startapp myapp the name field in apps/myapp/apps.py config class gets auto-populated with just myapp rather than the required apps.myapp which will throw a django.core.exceptions.ImproperlyConfigured exception unless a user manually updates the name field.

I understand if that was passed over but I wanted to ensure I was drawing attention to the proper aspect of the issue. It has been awhile since I was developing with Django as well and wasn't sure when this issue might have been introduced. I just didn't remember it being an issue in previous versions < 3.

Replying to Mariusz Felisiak:

Thanks for the report, however it works exactly the same in Django 3.2, 3.1, etc. I don't see any change in this behavior. AppConfig.name must be a full Python path to the application, as documented, so in your case Apps.bestapp is required and expected. We tried to improve generated apps.py files in #30618 but it was clunky.

Duplicate of #30618.

Last edited 3 years ago by Zack West (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top