26

I'm upgrading to django 3.2 but as per it's release notes, it says:

The SECRET_KEY setting is now checked for a valid value upon first access, rather than when settings are first loaded. This enables running management commands that do not rely on the SECRET_KEY without needing to provide a value. As a consequence of this, calling configure() without providing a valid SECRET_KEY, and then going on to access settings.SECRET_KEY will now raise an ImproperlyConfigured exception.

Error: django.core.exceptions.ImproperlyConfigured: Cannot import 'users'. Check that 'apps.users.apps.UsersConfig.name' is correct.

Users apps.py:

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'users'

I think that error is because of such as it was working with django==3.1.7. Can someone help me to solve this? How can i check if my secret key is valid or not and also generate new if needed?

vivekpadia70
  • 1,039
  • 3
  • 10
  • 30
  • Is the SECRET_KEY configured in your settings.py file? Did you provide default value for it? For example it should be similar to this: SECRET_KEY = env('DJANGO_SECRET_KEY', default='xxxxxx') Also, provide the full error message – Zhivko Zaikov Apr 12 '21 at 10:21
  • Added full error. Settings has this line for SECRET_KEY. SECRET_KEY = os.environ.get('SECRET_KEY') – vivekpadia70 Apr 12 '21 at 10:52
  • 2
    Check how "users" is defined and provide the relevant code – Zhivko Zaikov Apr 12 '21 at 11:01
  • 1
    Upvoting the question. I had exact same issue. Yes this started from 3.2. – lapin Apr 24 '21 at 12:02

4 Answers4

48

I don't think your error is directly related to that SECRET_KEY change in your question.

AppConfig.name should be the full Python path to the application. Since you appear to have users inside an apps module, you should use 'apps.users' instead of 'users'

class UsersConfig(AppConfig):
    name = 'apps.users'
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 1
    This worked. Thanks. But is this introduced in Django 3.2? I was using same code since django 2.1. – vivekpadia70 Apr 12 '21 at 12:28
  • 2
    You should have had `name = 'apps.users'` in Django 2.1 as well. There isn't enough code in your question to reproduce the problem, so we can't tell why it worked on previous versions then stopped working in Django 3.2. There are changes to [app config discovery](https://docs.djangoproject.com/en/3.2/releases/3.2/#automatic-appconfig-discovery) in Django 3.2, so perhaps it was something related to that. – Alasdair Apr 12 '21 at 14:12
8

If your django applications are in a folder like (app, apps, applications, etc..), you have to specify the absolute path in your app_name/apps.py file to the name attribute, see the image below.

Fugure 1.1 django apps vscode demo

On this image my users (1) app is in the app folder that's why a have to use app.users (3), (4).

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
6

I am having this issue suddenly with Django 3.2. Why now I am not exactly sure, but adding this to my settings.py file fixed it for me. I need to understand the structural issue causing this. It might be because our Django project contains many apps grouped together.

from django.apps import AppConfig

AppConfig.default = False

https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig.default

You can also set this individually on at an app level. This may be a better solution. I am still missing the reasoning for why this is necessary.

from django.apps import AppConfig


class DashboardConfig(AppConfig):
    name = 'dashboard'
    default = False
Sherd
  • 468
  • 7
  • 17
  • 1
    Do no forget to import AppConfig first `from django.apps import AppConfig` – Andres Xavier Vargas Vera May 03 '21 at 16:13
  • 1
    I don’t think this is a good idea in general. You are setting the default to `False` for all app configs that subclass `AppConfig`. There isn’t enough information in your answer to tell why you had to do this, but I think there is probably a better solution. – Alasdair May 07 '21 at 17:57
  • I don't disagree, but the change in Django 3.2 isn't documented well enough for me to know what exactly in my setup is wrong that caused my app to fail to run in the jump from 3.1 to 3.2. The proper solution may be to add a setting to all of my AppConfig classes for all of the 'apps' I have linked together in Django, but the documentation doesn't give a clear reason why this change was necessary. – Sherd May 11 '21 at 19:42
  • Using `default = False` = it is completely ignored and Django uses settings by default. Make sure your use the correct path to the module (`dashboard` in your case) **in relation to the manage.py file**. So if you `app.py` is located in `foo/bar/app.py` -> `name = 'foo.bar.apps.your_app'`. – 0leg Jun 24 '21 at 07:31
5

The solution for me was to simply delete the apps.py file entirely - Django now managed to find the app automatically.

Pedro A
  • 3,989
  • 3
  • 32
  • 56