从Flask获取Heroku变量 [英] Acess Heroku variables from Flask
问题描述
使用以下命令在Heroku配置文件中设置了我的数据库变量
$ b $ $ $ $ $ $ $ $ $ $ $ $> heroku config:add server = xxx.xxx .xxx.xxx
heroku config:add user = userName
heroku config:add password = pwd
heroku config:add database = dbName
如何从我的app.py文件访问这些变量?
没有运气:
server = os.environ.get('server')
打印服务器
出口()
运行 foreman start
22:41:32 web.1 |用pid开始28844
22:41:32 web.1 |无
22:41:32 web.1 |用代码0退出
22:41:32 system |发送SIGTERM到所有进程
运行 heroku config
显示正确的变量。
如果您在本地运行应用程序,可以下拉Heroku环境变量通过运行:
heroku config:pull --overwrite
这将创建一个包含您的环境变量的本地 .env
文件。
如果您在运行应用程序之前在终端中运行 $ source .env
,这些变量将以类似的方式加载到您的环境中Heroku的功能。
此外,您的代码看起来不正确。
您通常想要访问的环境变量是这样的:
pre $ from os import environ
#如果MY_ENVIRONMENT_VARIABLE不存在,None将被打印。
print environ.get('MY_ENVIRONMENT_VARIABLE')
I have setup my database variables in the Heroku config using the following command
heroku config:add server=xxx.xxx.xxx.xxx
heroku config:add user=userName
heroku config:add password=pwd
heroku config:add database=dbName
How do I access these variables from my app.py file?
I have tried the following but no luck:
server = os.environ.get('server')
print server
exit()
this is returning nothing useful to the console when running foreman start
22:41:32 web.1 | started with pid 28844
22:41:32 web.1 | None
22:41:32 web.1 | exited with code 0
22:41:32 system | sending SIGTERM to all processes
Running heroku config
shows me the correct variables.
If you're running your app locally, you can 'pull down' the Heroku environment variables by running:
heroku config:pull --overwrite
This will create a local .env
file which contains your environment variables.
If you then run $ source .env
in your terminal before running your app, these variables will be loaded into the environment for you -- in a manner similar to what Heroku does.
Also, your code looks incorrect.
The way you typically want to access environment variables is like so:
from os import environ
# If MY_ENVIRONMENT_VARIABLE doesn't exist, None will be printed.
print environ.get('MY_ENVIRONMENT_VARIABLE')
这篇关于从Flask获取Heroku变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!