Decouple.UndefinedValueError: SECRET_KEY not found when using python-decouple in Django

I'm getting the following error when I try to run my Django project:

decouple.UndefinedValueError: SECRET_KEY not found.

I'm using python-decouple to manage environment variables. In my settings.py, I have:

from decouple import config
SECRET_KEY = config('SECRET_KEY')

I’ve already created a .env file in the root directory of my project with the following line:

SECRET_KEY=my-very-secret-key

But the error still appears. I’ve confirmed that the .env file exists and contains the SECRET_KEY.

Things I’ve already checked:

✅ .env is in the same directory as manage.py

✅ File is named .env, not something like env.txt

✅ There are no spaces around the equal sign (i.e., SECRET_KEY = my-key is incorrect)

✅ I’ve restarted the server after creating the .env file

Is there something I’m missing about how decouple loads the .env file in Django?

Any help would be appreciated!

Check Error traceback showing SECRET_KEY not found when using python-decouple

Вернуться на верх