Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 7, 2022 04:51 pm GMT

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

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 ospasswordvalue = os.getenv("Password")

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


Original Link: https://dev.to/vulcanwm/environment-variables-in-heroku-python-385o

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To