Initializing settings.py after moving to environment variables

I’m working on loading my Django settings via environment variables to make pushing changes to production easier. To do so, I’m using django-environ. If I launch my website a very specific way via my IDE it works just fine, but if I launch my site via the command line my settings are properly being loaded and saved. I’ve included more specifics below, as well as some troubleshooting steps I’ve already taken.

My best guess is that it has to do with my directory structure, but I’m not sure what the problem is.

How I Successfully Launch App Via IDE Debugging
If I open my Django base directory in my IDE (VS Code using the Python extension) and launch debugging, the site loads just fine. Stepping through the calls to settings.py I can see 2 calls to settings.py. However, printing the process id with os.getpid() shows that it’s two different processes going through settings.py so that’s expected.

How I Fail to Launch App
When using the IDE debugging, opening the folder is essential - if I launch debugging with a single file open and manually specify the location of my manage.py, the site fails to load. If I attempt to launch the site via the command line arguments “python manage.py runserver” or “python manage.py runserver --noreload” it also fails.

In both of these failed attempts, the error is the same: “django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable.” When launching the app fails, I see that settings.py is called 3 times, all form the same process.

What I’ve already checked

  1. IDE Specific settings - I’ve looked at both the user and workspace settings for VS Code, but none appear to be relevant. I also commented them all out, and the result was not impacted.

  2. Directory structure - My current directory structure is listed below. I tried moving my app into the project folder in it’s own apps/${app_name} folder, but that didn’t resolve the issue.

  3. System Path - I used sys.path to print the system path at the time of executing settings.py in both situations and diff’ed them with Kdiff3, but they were exactly the same.

  4. Ensuring main directory is package - I tried adding init.py to the main directory to ensure it was considered a package, but that didn’t impact the results either.

Current Directory Structure
Project

  • .env

  • .env.template

  • .gitignore

  • db.sqlite3

  • LICENSE

  • manage.py

  • print_directory.py

  • env/

    • development.env
    • production.env
    • readme.txt
    • staging.env
  • App/

    • init.py
    • <App_files>
  • media/

    • <media_files>
  • Project/

    • init.py
    • settings.py
    • urls.py
    • wsgi.py

Screenshot of Beginning Portion of Settings.py

Make sure it’s not init.py but __init__.py, maybe that produces a conflict.

They actually are __init__.py I just forgot mark the text as preformatted so it was converted to bolding the init text.