2

I'm just new in this site and new to all the GNU/Linux environment, I'm trying to access to an environment variable from a python program that I wrote in PyCharm, I created it but I the the variable doesn`t appears in my python program.

This is what I already have done:

  • created an environment variable in .bashrc (when I verify, the variable appears me in the terminal)

  • This is what I have typed in my .bashrc file:

    export API_KEY=my_api_key_here

  • import os package in my Python program

  • use the os.environ module in my program (to check if my environment variable appears)

I don't really know where to define my environment variables, on .bashrc or .profile files, (that two option are the only ones that appears me)

In short I want to have access to my own defined environment variables from my program.

I hope you can help me please! And tell me what I'm doing wrong.

5
  • Are you sure you created an environment variable, not just a plain shell variable? Please edit your question to show the relevant line(s) of your .bashrc file Apr 15, 2021 at 0:46
  • Did you export the variable?
    – waltinator
    Apr 15, 2021 at 1:12
  • Yes, I typed it as shown above Apr 15, 2021 at 3:55
  • Can you see the variable in the shell,before starting python ? Try echo $API_KEY
    – Soren A
    Apr 15, 2021 at 7:15
  • Yes, in fact I checked it before I tried to use it in PyCharm do you think is something related with global / user variables? Apr 20, 2021 at 1:11

1 Answer 1

0

In Ubuntu, you usually put it in .bashrc, because users usually use bash. If you've verified that it exists in the terminal, probably you forgot to export it. Without exporting it, it exists only in your current terminal, not in your python script nor any program you calls from the terminal.

This is an example python code named envi.py:

import os
print(os.environ['MYVAR'])

First, let's test it directly in terminal:

export MYVAR="my value"
python3 envi.py
# the output should be: my value

If you're satisfied with the result, you can then put export MYVAR="my value" in .bashrc, open a new terminal, and then run your python script.

4
  • Thanks for helping me, I already did what you mentioned above and It got well I did not have any problem, then I typed the same on .basrc (export API_KEY=my_api_key_here) and I tried to run my code from PyCharm but Im still receiving the error message "raise KeyError(key) from None KeyError: 'API_KEY' ". Note: Im not running my python program from terminal, I`m running it from PyCharm just to check if my code has not any problem so far and I got this problem. THANK YOU! Apr 15, 2021 at 3:50
  • @ALDOFLORESAGUAYO I've not used pycharm for a long time. It seems pycharm doesn't read .bashrc, and setting API_KEY seems more suitable to be done in pycharm settings, please check this answer out. May it helps.
    – fikr4n
    Apr 16, 2021 at 1:35
  • I really appreciated it, I will try it and if I get some favorable results I will let you know again thanks a lot! Apr 20, 2021 at 1:14
  • When using pycharm, the environment variables have to be defined in "Run/Debug Configurations" dialog. In the python configuration inside the region "Environment" you can define environment variables that are passed to the program.
    – kap
    Aug 2, 2021 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .