dkobrin Posted Tuesday at 01:00 AM Posted Tuesday at 01:00 AM Hi, Thanks for setting all this up and taking the time to support all of us! My questions is regarding properly setting up environmental variables for Flask. I have set up a sample (simplified) project to iron this issue out. https://flask.dkobrin.helioho.st/MyNewFlaskApp/ I have looked through the discord for advice on this issue and have located my .env file in a non-public folder /home/environments/MyNewFlaskApp/.env and added the path to my load_dotenv (and for debugging dotenv_values), but although pathlib verifies the file exists, there are no key value pairs loaded. I set the permissions for the .env to 644 although I think it should be 640... here is the server import sys from flask import Flask, __version__ from dotenv import load_dotenv, dotenv_values from os import environ as env from getenv_path import env_path # load environment vars load_dotenv("/home/dkobrin.helioho.st/environment/MyNewFlaskApp/.env") #env_path) config = dotenv_values("/home/dkobrin.helioho.st/environment/MyNewFlaskApp/.env") #env_path) config2 = dotenv_values("/home/environment/MyNewFlaskApp/.env") if env_path.exists: print(env_path) print('FOO is') print(env.get('FOO')) print(config) print(config2) app = Flask(__name__) application = app @app.route("/") def hello(): if env_path.exists: print(env_path) print('FOO is') print(env.get('FOO')) print(config) print(config2) return """ Flask is working on HelioHost.<br><br> <a href="/flasktest/python/version/">Python version</a><br> <a href="/flasktest/flask/version/">Flask version</a> """ @app.route("/python/version/") def p_version(): return "Python version %s<br><br><a href='/flasktest/'>back</a>" % sys.version @app.route("/flask/version/") def f_version(): return "Flask version %s<br><br><a href='/flasktest/'>back</a>" % __version__ if __name__ == "__main__": app.run() the sample base code does work so the .htaccess and flask.wsgi seem to be ok here is my log output I feel like I'm missing something simple here. Thanks again! Quote
dkobrin Posted 22 hours ago Author Posted 22 hours ago Soo as it turns out, on Jonny you can safely place your .env file in the root of your app and the server will automatically prevent it from being served to a client. for this sample project, the path is load_dotenv("/home/dkobrin.helioho.st/flask.dkobrin.helioho.st/MyNewFlaskApp/.env") as an extra layer of safety I am also adding the following to my .htaccess file instructing Apache to deny requests for .env files #hide .env files from http access <FilesMatch ".env"> Order allow,deny Deny from all </FilesMatch> 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.