Fra*_*nco 5 python heroku flask
我已经使用以下命令在 Heroku 配置中设置了数据库变量
heroku config:add server=xxx.xxx.xxx.xxx
heroku config:add user=userName
heroku config:add password=pwd
heroku config:add database=dbName
Run Code Online (Sandbox Code Playgroud)
如何从 app.py 文件访问这些变量?
我尝试过以下方法但没有运气:
server = os.environ.get('server')
print server
exit()
Run Code Online (Sandbox Code Playgroud)
运行时这不会向控制台返回任何有用的内容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
Run Code Online (Sandbox Code Playgroud)
运行heroku config显示了正确的变量。
如果您在本地运行应用程序,则可以通过运行以下命令来“下拉”Heroku 环境变量:
heroku config:pull --overwrite
Run Code Online (Sandbox Code Playgroud)
这将创建一个.env包含环境变量的本地文件。
如果您在$ source .env运行应用程序之前在终端中运行,这些变量将为您加载到环境中——类似于 Heroku 的方式。
另外,您的代码看起来不正确。
您通常想要访问环境变量的方式如下:
from os import environ
# If MY_ENVIRONMENT_VARIABLE doesn't exist, None will be printed.
print environ.get('MY_ENVIRONMENT_VARIABLE')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2522 次 |
| 最近记录: |