setting environment variables in heroku for flask app

前端 未结 2 800
名媛妹妹
名媛妹妹 2020-12-30 12:48

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

app.conf         


        
相关标签:
2条回答
  • 2020-12-30 13:25

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

    heroku config:set IS_HEROKU=True 
    

    Then in your file

    import os
    is_prod = os.environ.get('IS_HEROKU', None)
    
    if is_prod:
        #here goes all your heroku config
    
    0 讨论(0)
  • 2020-12-30 13:28

    I don't know anything about your project but Heroku has a system for this.

    Instead of storing your production (Heroku) config vars in a file, you enter them using the command line or the web UI.

    Command line method:

    heroku config:set THEANSWERTOEVERYTHINGEVER=42

    I like the web UI method because it's pretty (it's in app settings).

    How you'll manage your development config vars is you'll write them YAML style in the .env file

    # Contents of .env file in application root (GITIGNORE THIS)
    
    # These are only for your development environment
    
    THEANSWERTOEVERYTHINGEVER=42
    ENVIRONMENT="DEVELOPMENT"
    

    Then in your application file add import os

    You can get config variables using this syntax os.environ.get('THEANSWERTOEVERYTHINGEVER')

    Last but most important step!

    Launch your server with heroku local instead of python mysweetapp.py. This will launch Heorku's server and load your local config vars for you. Probably requires Heroku Toolbelt if you don't have it.

    It's all here. https://devcenter.heroku.com/articles/getting-started-with-python#define-config-vars

    0 讨论(0)
提交回复
热议问题