user5819768
user5819768

Reputation:

Why does python not see the environment variable

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

Answers (1)

user5819768
user5819768

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

Related Questions