[Python] Django on PythonAnywhere: ImproperlyConfigured: The SECRET_KEY setting must not be...

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I’m hosting a Django project on PythonAnywhere and encountering the following error when running makemigrations: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

    My Setup:

    Hosting Platform: PythonAnywhere
    Environment Management: Using a .env file to manage secrets
    Python Version: 3.10
    Django Version: 5.1.2

    In my WSGI file, I’ve included the following code to load the .env file:

    import sys
    from dotenv import load_dotenv

    path = '/home/verbsmerch/verbs_backend'
    project_folder = os.path.expanduser(path)
    load_dotenv(os.path.join(project_folder, '.env'))


    In my settings I load the SECRET_KEY like this:
    SECRET_KEY = os.getenv("SECRET_KEY")

    What I’ve Tried:

    1. Verified that the .env file exists and is located in the correct directory.
    2. Checked that the .env file has the correct permissions (chmod 600).
    3. Ran a Python shell to debug:

    from dotenv import load_dotenv
    import os

    load_dotenv('/home/verbsmerch/verbs_backend/.env')
    print(os.getenv("SECRET_KEY")) # This prints the correct key


    1. Reloaded the web app from the PythonAnywhere Web tab.

    Why is Django unable to load the SECRET_KEY when it seems to work in the Python shell? How can I resolve this error?

    Continue reading...

Compartilhe esta Página