I want to set an environment variable DATABASE_URL that will be read by my Flask app to connect to the database. I use set DATABASE_URL = '...', but I get an error that the variable is not set when I do flask run. Why isn't this working?import osfrom flask import Flaskfrom sqlalchemy import create_engineapp = Flask(__name__)if not os.getenv("DATABASE_URL"): raise RuntimeError("DATABASE_URL is not set")engine = create_engine(os.getenv("DATABASE_URL"))I navigate to the installed directory for project1 and do the following in Windows 10 cmd.exe:set FLASK_APP = application.pyset FLASK_DEBUG = 1set DATABASE_URL = 'postgres.......' (the credential given by the Heroku account)flask runFlask runs, I go to localhost:5000, and get the following error:Traceback (most recent call last): File "c:\program files\python36\lib\site-packages\flask\_compat.py", line 35, in reraise raise value File "C:\Program Files\Python36\learningPython\web_CS50\project1\application.py", line 12, in <module> raise RuntimeError("DATABASE_URL is not set")RuntimeError: DATABASE_URL is not set 解决方案 Just remove spaces from set commandset FLASK_APP=application.pyset FLASK_DEBUG=1set DATABASE_URL='postgres.......'More at https://ss64.com/nt/set.html 这篇关于在Windows中运行Flask看不到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 00:16
查看更多