1


I have some env vars which should be loaded when the the specific func triggers, but when I run the main.py in Pycharm it does not work and gives the below error:
'Error Message': 'the parameter apikey is invalid or missing'

I have exported the env var in terminal:
$ export ALPHAVANTAGE_API_KEY=1qa2ws3ed4rf5t

and checked the env to see if it is there.

and tried to get it in the code:
alphavantage_api_key = os.environ.get("ALPHAVANTAGE_API_KEY")

but it gives me error, the interesting thing is if I run the main.py in terminal or hard coded the API-Key, everything works perfectly.

this is the code:

import os
import requests


# variables
STOCK = "something"
COMPANY_NAME = "something"
alphavantage_url = 'https://www.alphavantage.co/query'
alphavantage_api_key = os.environ.get("ALPHAVANTAGE_API_KEY")
alphavantage_parameters = {
    'function': 'TIME_SERIES_DAILY',
    'symbol': STOCK,
    'apikey': alphavantage_api_key,
    'datatype': 'json'
}

# Getting info from AlphaVantage
response = requests.get(alphavantage_url, params=alphavantage_parameters)
response.raise_for_status()
response.json()
print(response.json())
Arash Khosravi
  • 103
  • 1
  • 7
  • 5
    Setting an env var from a terminal only affects the shell running in that terminal, and subshells or other programs launched from that shell. It wouldn't be visible in PyCharm unless you ran PyCharm from the same shell, after setting the var. – jasonharper Jan 20 '21 at 17:13
  • @Szellem, thanks a lot it solved the problem – Arash Khosravi Jan 22 '21 at 12:30

0 Answers0