Forums

django-configurations and PythonAnywhere

I'm using django-configurations in my solution to separate dev settings from prod. As part of that in dev, I modify the manage.py as follows:

#!/usr/bin/env python

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
    os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')

    from configurations.management import execute_from_command_line

    execute_from_command_line(sys.argv)

And I use environment variables to override. I made the same pages to my PA which has a few more values for production.

import os
import sys
from configurations.management import execute_from_command_line

os.environ['AWS_ACCESS_KEY_ID'] = '<SNIP>'
os.environ['AWS_SECRET_ACCESS_KEY'] = '<SNIP>'
os.environ['DJANGO_SECRET_KEY='] = '<SNIP>'
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
os.environ['DJANGO_CONFIGURATION'] = 'Prod'

execute_from_command_line(sys.argv)

But when I refresh my web app, I get the following error:

Error running WSGI application
django.core.exceptions.ImproperlyConfigured: Configuration cannot be imported, environment variable DJANGO_CONFIGURATION is undefined.

Any ideas what I'm doing wrong here? I did pip install django-configurations. I also made the same environment variables in /postactive and they work.

Thanks a lot for the help!

We have a few tips on working with env vars here, have you seen them? Do they help? https://help.pythonanywhere.com/pages/environment-variables-for-web-apps

Yes, I have seen that link, thank you. It's always good to have a second look a few hours later though and having done, I don't see anything in my WSGI file that looks out of place.

Do you have any other suggests?

Thank you !

Maybe you need to set your environment variables before you import configurations?

My issue was two-fold:

  1. Having a folder structure which is REPRO/PROJECT/APP This is common when you want to have several things in your repo (like your /docs). Anyway... correcting the Path fixed this.
  2. Also, the changes for DJANGO-CONFIGURATIONS I made to my wsg.py were incorrect. I brought over the changes I had made to manage.py instead of the correct changes for wsgi.py.

The two issues combined made the whole thing very hard to figure out. Thanks to you all for your help as well!

Here is the corrected wsgi.py for DJANGO-Configurations were your Django project is in in a sub folder. The correct Source Directory and Working Directory on the web app is "/home/user/repo/project/"

import os
import sys

path = '/home/user/repo/project/'
if path not in sys.path:
    sys.path.append(path)

os.environ.setdefault('AWS_ACCESS_KEY_ID', '<SNIP>')
os.environ.setdefault('AWS_SECRET_ACCESS_KEY', '<SNIP>')
os.environ.setdefault('DJANGO_SECRET_KEY', '<SNIP>')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Prod')

from configurations.wsgi import get_wsgi_application
application = get_wsgi_application()

Great! Glad you figured it out.