3

I'm working on flask apps for simple websites. Currently, all my secret keys and api keys are in a config.py file in my github repo and I configure the flask app via

app.config.from_object('config.Config')

The github repo is private, but this is worst practice. I want to share my github repo with others to collaborate. First, I have to hide all my keys, then I can make the repo public (or create a new one that has never had the keys in it) and then share the repo.

Now I want a boolean to decide how to config, for which I'm currently using:

is_heroku = environ.get('IS_HEROKU', None)
if is_heroku:
    app.config.from_prefixed_env()
else:
    app.config.from_object('config.Config')

I set a bunch of heroku environment variables in the GUI like so: picture of environment variables in heroku

Supposedly, if I begin all my environment variables with "FLASK_" and then use app.config.from_prefixed_env(), it will configure using those automatically. It has not. I am not sure what is wrong here or if I should be configuring differently with heroku. The local configuration still works fine.

I'm sorry that I'm quite bad with computers and web development. I'm a math postdoc who is trying to learn programming as a hobby.

Leo Herr
  • 81
  • 4

2 Answers2

2

Make sure you have set the IS_HEROKU value in the HEROKU config variables either by the CLI or GUI method mentioned below... also make sure you run the heroku version and not the inbuilt development server for the Heroku config variables to work.

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 through the web UI.

Heroku Command line method:

heroku config:set THEANSWERTOEVERYTHINGEVER=42

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

How you'll manage your development config vars is you'll write them in YAML format 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 myapp.py. This will launch Heroku's server and load your local config vars for you. Probably requires Heroku Toolbelt if you don't have it.

The link to official documantation: https://devcenter.heroku.com/articles/getting-started-with-python#define-config-vars

More on how to use the .env file as for the environment variables

https://medium.com/thedevproject/start-using-env-for-your-flask-project-and-stop-using-environment-variables-for-development-247dc12468be

link to originally answered: https://stackoverflow.com/a/32321268/8401179

SANGEETH SUBRAMONIAM
  • 1,098
  • 1
  • 9
  • 10
1

With Heroku the best place to store your API keys and secret keys are here:

https://dashboard.heroku.com/apps/YOURAPPNAME/settings

Then use OS to call the config vars (Heroku's name for them) or environmental variables as they are universally known. How to set environment variables in Python?

Here's how to distinguish configuration (depending on whether you are testing via localhost or whether your app is live):

import os

ENV = 'LIVE'

if ENV == 'dev':
    ...
    # Store your secret keys and API keys by creating a local .env file. 
    
if ENV == 'LIVE':
    sitekey = os.environ.get('GOOGLE_SITE_KEY')
    # Heroku will default to Settings > Config Vars

enter image description here

Olney1
  • 572
  • 5
  • 15