Reputation:
In the terminal, I have exported my API key the following way:
export ALPHAVANTAGE_KEY=XXXXXXXXXX
In the console, when I type 'env' I get amongst other things :
ALPHAVANTAGE_KEY=XXXXXXXXXX
But in my code, the following prints 'None' :
print(os.environ.get('ALPHAVANTAGE_KEY'))
Why is that ?
Upvotes: 0
Views: 1574
Reputation:
A nice way to manage the environment variables is with dotenv:
from dotenv import load_dotenv # pip install python-dotenv
load_dotenv("/Users/gerald/environment_variables/.env")
With the .env file looking like this :
ALPHAVANTAGE_KEY="XXXXXXXXXXXXX"
NEWSAPI_KEY="YYYYYYYYYYYYY"
And then use the values this way :
"apikey": os.environ.get('NEWSAPI_KEY')
Upvotes: 2