Forums

Environment variable not working in Django app

I set up a Django app using the PythonAnywhere wizard and also set up a virtual environment for it.

I am having issues setting up environment variables. I have followed the instructions https://help.pythonanywhere.com/pages/environment-variables-for-web-apps

I am trying to set a DEBUG environment variable, my ~/yogasite/.env reads as follows: DEBUG = False . However, the DEBUG value is still set to true.

I have used the link in the web tab to edit the wsgi file, located somewhere in the /var/www/ path. I added the following text:

from dotenv import load_dotenv
project_folder = os.path.expanduser('~/yogasite')  # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))

There is another wsgi file located in the yogasite subfolder of my project folder, wsgi.py file, am I supposed to edit that file at all?

I used DEBUG = os.getenv("DEBUG") in my settings.py file.

It appears to have no effect though, because the site still continues to report that the DEBUG environment variable is set to True.

thanks for your help,

It sounds like your setup is correct -- the WSGI file in /var/www is the only one you need to change.

However, one thing that looks a bit wrong is this:

DEBUG = os.getenv("DEBUG")

The os.getenv function returns a string, so your code is likely to be setting the DEBUG environment variable to the string "False" rather than to the boolean False, and non-empty strings are "truthy" in Python.

Try this and see if it helps:

DEBUG = os.getenv("DEBUG") == "True"

That works, thanks giles.

I was not aware of the fact that os.getenv would always return a string, so that clears that up.

So I guess the other workaround to work with boolean environment variables using this method is to set the environment variable to an empty string (when False is desired) in the .env file, is that correct?

The book I am reading on Django suggests using the environs (https://github.com/sloria/environs) package instead of python-dotenv. That package does appear to offer a way to obtain a variable of the boolean type. Is there a way to work with environs in PythonAnywhere?

thanks very much for your help.

Sure, give environs a try.

Hello everyone.

I am still trying to figure out how to use environment variables with my Django app.

How would I load environment variables in my local web app? I can add (to my settings.py):

from dotenv import load_dotenv

project_folder = os.path.expanduser('~\Documents\projects\yoga-site')
load_dotenv(os.path.join(project_folder, '.env'))

But then this path only applies to my local app? This would not work in my PythonAnywhere online app. And I would like to have a single settings.py file working for both my local and web app. Am I right in adding this code to my settings.py file locally?

I am really confused about the basic usage of environment variables with PythonAnywhere.

thank you,

Normally you'd put something in your WSGI file on PythonAnywhere with the load_dotenv. That would be outside your code tree, so you wouldn't have it in your repo. Then locally you'd set the environment variables in your machine's system settings, or somewhere similar. That would mean that in settings.py, you would just use os.environ without worrying about how the environment variables got there.

Thanks giles. I understand now, I remember doing this a different way before (using a local .env file as well) but I got it to work by setting environment variables in my local os, as you suggested. Had to learn how to set environment variables in Windoze though.

If I wanted to try the 'environs' package, would I use similar code in my wsgi file on PythonAnywhere?

thanks very much.

environs looks pretty similar to python-dotenv to me, so I'd expect you'd be able to use it equally well.