Franco
Franco

Reputation: 2926

Acess Heroku variables from Flask

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.

Upvotes: 5

Views: 2528

Answers (1)

rdegges
rdegges

Reputation: 33824

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')

Upvotes: 2

Related Questions