Why PyCharm does not recognize user environment variables configuration?

Question:

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'



Asked By: MikaM

||

Answers:

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.

Answered By: wberry

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 🙂

Answered By: MikaM

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

Answered By: ninja_1823