在Heroku中为flask应用设置环境变量 [英] setting environment variables in heroku for flask app

查看:107
本文介绍了在Heroku中为flask应用设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个flask应用程序,该应用程序将不同的配置文件用于开发和生产环境.相关的代码段是这样的:

I have a flask application that uses different configuration files for development and production environments. The relevant piece of code is this:

app.config.from_object('config.dev')
app.config.from_envvar('SPOTPIX_SETTINGS', silent=True)

当我在本地服务器上进行开发时,配置取自config.dev,但是当我将代码推送至heroku时,我想将SPOTPIX_SETTINGS环境变量设置为指向config.prod文件.可以这样在heroku命令行客户端中完成:

When i'm developing in my local server, the configurations are taken from config.dev, but when i push the code to heroku, i would like to set SPOTPIX_SETTINGS environment variable to point to the 'config.prod' file. This can be done in heroku command line client like so:

heroku config:set SPOTPIX_SETTINGS= 

但是,我不知道应该在等号右边写什么,因为我无法将生产配置文件的绝对路径分配给环境变量,因为在heroku中这与它不一样在我的开发机器中.

However, I have no clue what should i write to the right of the equals sign, as i can't assign the absolute path of the production configuration file to the environment variable, because this is not the same in heroku as it is in my development machine.

非常感谢!

推荐答案

您应该使用env variable来查看您是否在dev或heroku中.

You should use a env variable to look if you are in dev or heroku.

heroku config:set IS_HEROKU=True 

然后在您的文件中

import os
is_prod = os.environ.get('IS_HEROKU', None)

if is_prod:
    #here goes all your heroku config

这篇关于在Heroku中为flask应用设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆