( django.core.exceptions.ImproperlyConfigured: Cannot import 'apps.accounts'. Check that 'mysite.apps.accounts.apps.AccountsConfig.name' is correct
Asked Answered
L

4

19

This is how it is structured The apps.py file is under apps which is under mysite The code inside apps.py of accounts folder file is

from django.apps import AppConfig
class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = "apps.accounts"

The code inside Settings is

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mysite.apps.accounts',
]

I tried changing 'mysite.apps.accounts', to 'mysite.apps.AccountsConfig', and changing name = "apps.accounts" to name = "accounts" I am new to Django and was following How to make a website with Python and Django - MODELS AND MIGRATIONS (E04) tutorial. Around 16:17 is where my error comes up when I enter python manage.py makemigrate to the vscode terminal The error is

ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'apps.accounts'. Check that 'mysite.apps.accounts.apps.AccountsConfig.name' is correct. Someone please help me.

Leyden answered 18/3, 2022 at 15:53 Comment(0)
L
2

The solution was quite counterintuitive. You have to delete the

class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = "accounts"

from apps.py\accounts\apps\mysite. Then run python manage.py makemigrations and 2 new models 'UserPersona' and 'UserProfile' are created. the output in the terminal:

mysite\apps\accounts\migrations\0001_initial.py
    - Create model UserPersona
    - Create model UserProfile
Leyden answered 19/3, 2022 at 10:27 Comment(1)
Sorry to bother, but didn't that modify or affect the functionality of your site as a whole? I'm having the same issue as you, I believeFabrianna
R
43

The name in apps.py should be the same (value) that you put in INSTALLED_APPS (in settings.py). That's the correct one.

from django.apps import AppConfig

class AccountsConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "mysite.apps.accounts"

settings.py code:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mysite.apps.accounts',
]
Resistencia answered 4/7, 2022 at 10:35 Comment(0)
L
2

The solution was quite counterintuitive. You have to delete the

class AccountsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = "accounts"

from apps.py\accounts\apps\mysite. Then run python manage.py makemigrations and 2 new models 'UserPersona' and 'UserProfile' are created. the output in the terminal:

mysite\apps\accounts\migrations\0001_initial.py
    - Create model UserPersona
    - Create model UserProfile
Leyden answered 19/3, 2022 at 10:27 Comment(1)
Sorry to bother, but didn't that modify or affect the functionality of your site as a whole? I'm having the same issue as you, I believeFabrianna
C
0

The name in AppConfig should be same as the name provided in Installed Apps i.e., it should be "mysite.apps.accounts".

Cottager answered 31/1, 2023 at 7:36 Comment(0)
K
0

You can add your apps package to the sys.path, so you don't need to worry about the name, and just include the name of the app in INSTALLED_APPS as always:

import sys
sys.path.insert(0, str(BASE_DIR / 'apps'))
INSTALLED_APPS = [
    # ...
    'accounts',
    # ...
]
Kayak answered 20/6, 2024 at 10:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.