Python/Django: Updated .env Values Not Reflected After Restarting runserver

I'm working with a Django project and using a .env file to manage my environment variables. However, after updating values in the .env file and restarting the server (python manage.py runserver), the new values are not being reflected in the application. Specifically, the SLACK_CLIENT_ID and SLACK_APP_NAME values are not being updated even though I can confirm the .env file is loaded correctly.

Here’s what I’ve done:

  1. I updated the .env file with the correct values.
  2. I restarted the Django development server using python manage.py runserver.
  3. I confirmed that the .env file is being loaded (printed debug statements for SLACK_CLIENT_ID and SLACK_APP_NAME).
  4. Despite these updates, the app still shows the old environment variable values (e.g., your-slack-client-id).

I’ve tried the following:

  1. Ensuring that the .env file is in the correct location and formatted properly.
  2. Using load_dotenv() to load the .env file in my settings.py.
  3. Restarting the server multiple times.
  4. Checking the virtual environment.
from dotenv import load_dotenv
import os

load_dotenv()

SLACK_CLIENT_ID = os.getenv('SLACK_CLIENT_ID', 'your-slack-client-id')
SLACK_APP_NAME = os.getenv('SLACK_APP_NAME', 'Slack Integration')

print("SLACK_CLIENT_ID from .env:", SLACK_CLIENT_ID) 

Even though I’ve removed the fallback value 'your-slack-client-id' and updated the .env file, the print statement still outputs the old value (your-slack-client-id). The new value isn't being picked up.

Any suggestions on what might be going wrong?

A few suggestions for debugging:

  • Check for shell environment variables (.bashrc, .bash_profile, .zshrc, .zprofile, etc...) to see if there are default values for these environment variables, or you could try load_dotenv(override=True) which should override using values in your .env file.

  • Make sure load_dovenv is loading correct .env file, this might be a good read/reference. What is the use of python-dotenv?

  • Check for typos in .env file.

  • If you are using virtual env, try restarting the IDE.

Looking again at your problem, I highly suspect that there is already definition on the environment variable from start up script (because os.getenv() returns a value instead of going into fallback).

Hope these could help!

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