2017-11-05 15 views
0

GrapheneDB 인스턴스에 액세스하기 위해 Heroku를 통해 몇 가지 환경 변수를 설정했습니다. Heroku CLI 명령 heroku config을 사용하면 모든 환경 변수가 예상대로 반환됩니다. 예를 들어os.environ.get()은 Heroku 환경 변수에 대해 None을 반환합니다.

, "heroku config" 반환 : 나는 이러한 환경 변수에 액세스 할 수 os.environ.get() 방법을 사용하려고하면

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 

그러나, 세 가지 인쇄 문은 오히려 heroku config 반환하는 원하는 출력보다 None을 반환합니다. 이것은 나에게는 Python 환경이 Heroku 환경 변수에 액세스 할 수 없음을 나타냅니다. 어떻게 파이썬에 액세스 할 수 있습니까?

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) 

나는 Acess Heroku variables from Flask 에서 솔루션을 사용하여 시도했지만 한 내가 명령을 수행 할 때 당신이 (env 또는 비슷한 이외의) 명령을 실행하기 때문에 heroku config:pull --overwrite CLI를 config:pull is not a heroku command.

답변

1

을 반환로를 그 설정 변수를 얻는다는 것은 보통 환경에 있지 않을 가능성이 높다는 것을 의미합니다. 즉, os.environ.get()을 통해 얻을 수 없다는 것을 의미합니다.

당신이 바로 그 명령의 출력에서 ​​그들을 추출하는 것입니다 수행 할 수있는 작업 (예 - 파이썬 2.7 - 그들은 또한 같은 방식으로 stderr을 확인하지 않는 경우가, stdout에 나올 가정) :

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 

주 :

  • 의 예는 그것뿐만 아니라 stderr을 확인하지 경우 정보가에, stdout에 나오는 가정 파이썬 2.7
  • 입니다 같은 방법으로
  • heroku 실행 파일의 전체 경로를 사용해야 할 수도 있습니다.