Forums

Failing to reload app when trying to load .env variable in WSGI

I have some environment variables stored under /home/postpulser/mysite/.env that I want my flask app to load. I followed this and this and my WSGI file now looks like this

# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project

import sys
import os
from dotenv import load_dotenv

# add your project directory to the sys.path
project_home = u'/home/postpulser/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# project_folder = os.path.expanduser('~/mysite')  # adjust as appropriate
load_dotenv('/home/postpulser/mysite/.env')

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application  # noqa

However, when I try to reload the app, it does not update, and I get: "Your webapp took a long time to reload. It probably reloaded, but we were unable to check it." .

The problem apparently lies in the load_dotenv('/home/postpulser/mysite/.env') line. When I remove it, the app reloads (but of course the .env variables are not loaded if I do so).

Any ideas on how to fix this?

Does the file that you're loading from exist? Is it a valid .env file?

Though it may not actually be the env file that is the issue. If you have code that behaves well when the environment is not set and behaves badly when the environment is set, then the issue could be in some other code somewhere else.