Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.environ.get() returns None for Heroku environmental variables

I have a few environment variables set up through Heroku to access a GrapheneDB instance. When I use the Heroku CLI command heroku config, all the environmental variables are returned as expected.

For example, "heroku config" returns:

GRAPHENEDB_BOLT_PASSWORD: some_password   
GRAPHENEDB_BOLT_URL:      bolt://hobby-someletters.dbs.graphenedb.com:24786
GRAPHENEDB_BOLT_USER:     appnumbers  
GRAPHENEDB_URL:           http://appnumbers:[email protected]:24789  
NEO4J_REST_URL:           GRAPHENEDB_URL

However, when I try to use the os.environ.get() method to access these environment variables, all three print statements return None rather than the desired output that the heroku config returns. This would indicate to me that the Python environment does not have access to the Heroku environment variables. How can I give python access to these?

import os
from py2neo import Graph

graphenedb_url = os.environ.get('GRAPHENEDB_BOLT_URL')
graphenedb_user = os.environ.get("GRAPHENEDB_BOLT_USER")
graphenedb_pass = os.environ.get("GRAPHENEDB_BOLT_PASSWORD")

print(graphenedb_url)
print(graphenedb_user)
print(graphenedb_pass)

I have tried using the solution from Acess Heroku variables from Flask but when I do the command: heroku config:pull --overwrite the CLI returns config:pull is not a heroku command.

like image 488
Tim Holdsworth Avatar asked Dec 04 '25 17:12

Tim Holdsworth


1 Answers

Because you are executing a command ( other than env or something similar) to get those config variables it means that most likely they are NOT in your normal environment, meaning you can't get them through os.environ.get().

What you can do would be to extract them from the output of that very command (the example - python 2.7 - assumes they come out on stdout, if they're not also check stderr in the same way):

from subprocess import Popen, PIPE

graphenedb_url = graphenedb_user = graphenedb_pass = None
stdout, stderr = Popen(['heroku', 'config'], stdout=PIPE, stderr=PIPE).communicate()
for line in stdout.split('\n'):
    split = line.split(':')
    if len(split) == 2:
        if split[0] == 'GRAPHENEDB_BOLT_URL':
            graphenedb_url = split[1].strip()
        elif split[0] == 'GRAPHENEDB_BOLT_USER':
            graphenedb_user = split[1].strip()
        elif split[0] == 'GRAPHENEDB_BOLT_PASSWORD':
            graphenedb_pass = split[1].strip()
print graphenedb_url
print graphenedb_user
print graphenedb_pass

Notes:

  • the example is python 2.7
  • it assumes the info comes out on stdout, if not check stderr as well, in the same way
  • you may need to use the full path for the heroku executable, not sure.
like image 170
Dan Cornilescu Avatar answered Dec 07 '25 05:12

Dan Cornilescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!