2

I tried to use the PyCharm user environment variable configuration, however, it throws KeyError. If I try to set the variables via commands it works, but via configuration it does not. I will appreciate any help.

import os

print(os.environ['BLA']) 



(venv) (base) mikam@Mikas-MacBook-Pro Scripts % python implementVar.py
Traceback (most recent call last):
  File "/Users/mikam/Desktop/Scripts/implementVar.py", line 2, in <module>
    print(os.environ['BLA'])
  File "/usr/local/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'BLA'



1
  • Personally, I've gone with using .env file and building my APIs to import it. Commented Jul 24, 2022 at 16:01

3 Answers 3

4

OK, apparently for terminal and run, there are different places where you define configurations.

During the whole time, I used the configurations at the top right corner, While there is another place in the terminal settings where it is also possible to configure the Terminal environment variables only.

Anyhow it solved the problem--> also necessary to restart Pycharm after adding each parameter. Hope it will help someone :)

Sign up to request clarification or add additional context in comments.

1 Comment

for zsh users, PyCharm will source .zshenv file. Make sure you restart!
0

Most likely your use of venv is intercepting these env variables. If you are on Windows, there is a separate feature in venv to set environment variables for the virtual Python environment. https://www.roelpeters.be/set-environment-variables-in-virtual-environment-python/

Alternatively, you could allow PyCharm to manage the Python interpreter outside of venv. PyCharm has a multiple interpreter management subsystem of its own. Combining them is probably somehow causing your problem.

Comments

0

I had the same problem, for me this solved it:

In the menu go to:

File > Settings > Build > Execution > Deployment > Console> Python > Console

Environmental Variables. --> Add your variables here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.