DEV Community

Medea
Medea

Posted on

Environment Variables in Heroku - Python

So I was messing around with Heroku trying to find out how to add environment variables without using the CLI (so setting the variables on the website), and after an hour I figured out how to do it.
Now I want to spread my knowledge so anybody else who wants to know how to set and access environment variables in Heroku using Python can do it easily.

So first go to https://dashboard.heroku.com/apps so you can see all the apps you have deployed.
It should look a bit like this:

dashboard

Now click on the app you want to set the environment variables on.
It should show something like this:

navbar

Then click on settings (which is on the right hand side of the navbar).
That should lead you to a page that looks like this:

settings page

Then click the button that shows Reveal Config Vars.
It should display something a page like this:

config

In the KEY input, enter the name of the environment variable, and in the VALUE input, enter the value of the environment variable.

You are done with the website side of the problem!


Now go to your code.
To access your environment variables which you stored in Heroku in Python, you only need to install the os module.



import os


Enter fullscreen mode Exit fullscreen mode

Then, the function to access to the environment variables is os.getenv("[key_name]") (replacing [key_name] with the key name).

For example, if you want to access the environment variable with the key name of Password, you would do:



import os
passwordvalue = os.getenv("Password")


Enter fullscreen mode Exit fullscreen mode

You've done it!
Now you can easily access environment variables in Heroku in Python.

Top comments (2)

Collapse
 
victorio profile image
Vic • Edited

Very useful for Open Source projects.

Collapse
 
vulcanwm profile image
Medea

Yep!