Updated 2022-05-22 1
Viewed 8 times
0

Of course, my Flask application works in my development environment but not on the server. Here are the relevant files:

|- application
|---- __init__.py
|---- config.py
|---- ...
|- wsgi.py
|- .env

Here's wsgi.py

from application import init_app

app = init_app()

if __name__ == "__main__":
    app.run("host=0.0.0.0)

Here's application/__init__.py

from flask import Flask

def init_app():
    # Initialize the core application
    app = Flask(__name__, instance_relative_config=True)

    # Select and load config object dynamically based on the value of FLASK_ENV in .env
    if app.config["ENV"] == "production":
        app.config.from_object("application.config.ProductionConfig")
    else:
        app.config.from_object("application.config.DevelopmentConfig")

    with app.app_context():
        return app

And finally .env

FLASK_APP=application
FLASK_ENV=development

I have verified that on my development machine, changing the value of FLASK_ENV in .env works as expected. On the server, it tries running as production regardless of the value. What am I missing?

Edit to add that this is an Ubuntu 20 server on AWS Lightsail if that's important.

🔴 No definitive solution yet
📌 Solution 1
0

The files are only loaded by the flask command or calling run(). If you would like to load these files when running in production, you should call load_dotenv() manually. checkout this

probably add flask.cli.load_dotenv() and also make sure python-dotenv is installed with pip install python-dotenv