maroxe
maroxe

Reputation: 2127

Impossible to access Heroku config vars from python code

I am trying to deploy my django app on Heroku, but the issue is that I cannot access my config vars from python code.

Here are my config vars (obfuscated for obvious reasons)

$ heroku config --remote production
=== myapp Config Vars
DATABASE_URL:                   postgres://<removed>
DJANGO_DEBUG:                   0
GOOGLE_APPLICATION_CREDENTIALS: <removed>
SECRET_KEY:                     <removed>

Now, in my django settings.py, the code

import os
os.environ["DJANGO_DEBUG"]

for example results in an error because the key "DJANGO_DEBUG" is nowhere to be found in the dictionary os.environ.

My question is: how can I access Heroku config variables in production?

I have tried the package python-decouple, but it is not able to access the config vars neither. (Locally, my variables are put in a .env file and are accessible with the help of the package python-decouple)

Edit: I actually realized that the config variables are accessible from my app and from the command if I run bash from within the app page on heroku, but there are not accessible if I run python on an ssh session, i.e., if I run python like this:

$heroku ps:exec
$python

os.environ does not contain my config variables.

Thank you for your help!

Upvotes: 3

Views: 2050

Answers (2)

maroxe
maroxe

Reputation: 2127

I am the original OP. Turns out the problem was the way I was accessing the remote terminal session. The correct way to do it to have all the config vars accessible is

heroku run bash

as opposed to

heroku ps:exec

Upvotes: 2

Eugene Upston
Eugene Upston

Reputation: 196

You can set the config vars for a Heroku application in the settings of the App. Click the Settings Tab then navigate to the Config Vars section. Click Reveal Config Vars and enter the key pair values for your variables.

These will get injected into your application.

Upvotes: 0

Related Questions