Not picking up settings file correctly in Django app

Getting ‘Settings’ object has no attribute ‘API_KEY’ as an error when trying to use API_KEY = os.environ[‘API_KEY’] in my settings file and then reference it like this: google_maps_key = settings.GOOGLE_MAPS_API_KEY in my code. Works fine in my heroku app, does anyone know how to fix? I’m using a custom settings file and I’ve ensured DJANGO_SETTINGS_MODULE env varible is set to the correct file.

I’m sure it’s loaded the settings file correctly as there was an unassigned variable error when building it located in the file that I’ve since changed but it just seems to not be able to get attributes on the settings object.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Try deleting the pyc files

best with pyclean

pip install pyclean

pyclean -v .

then restart the server

sudo systemctl restart gunicorn

Hi there,

There are several factors to consider when diagnosing this issue with your Django app deployed on DigitalOcean’s App Platform. Here are a few things that I could suggest checking:

  1. Environment Variables:

    • Ensure that the environment variable API_KEY is correctly set in your DigitalOcean’s App Platform deployment environment.
    • Access the dashboard, select your app, then select your Django service/component. In the component settings, there should be an option to manage environment variables. Make sure API_KEY is present.
  2. DJANGO_SETTINGS_MODULE Environment Variable:

    • Confirm that DJANGO_SETTINGS_MODULE is correctly set. Remember, if your settings file is located at myproject/, DJANGO_SETTINGS_MODULE should be set to myproject.mysettings.
  3. Check Settings File:

    • Ensure there’s no typo in the settings file. It’s easy to overlook the name of the variable. For example, if you define API_KEY, make sure you’re not referencing it as GOOGLE_MAPS_API_KEY or vice-versa.
  4. Error Location:

    • The error message seems to indicate that somewhere in your code, you’re attempting to access settings.GOOGLE_MAPS_API_KEY. If API_KEY is the name in your settings file, then you should be accessing settings.API_KEY in your code. Verify you are accessing the correct variable name.
  5. Settings File Loading:

    • If you have multiple settings files or configurations, ensure that the correct file is being loaded for the environment you’re running on App Platform. You can add a print statement or a logger in your settings file to confirm if it’s being loaded.
  6. Local Testing:

    • Test locally by setting the environment variables API_KEY and DJANGO_SETTINGS_MODULE to mimic the production environment, and see if you can reproduce the error.
  7. Caching and Deployment:

    • Sometimes, old builds or caching can cause issues. Try re-deploying your app after clearing any cache or build artifacts.

Let me knwo how it goes!


Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel