-1

I want to use a value from an environment variable. I have set it in the ~/.bashrc file and I am able to see that in the new shells and in the current shell(obviously, after sourceing it). However, when I import the same variable in Python3 shell(both in the existing and in the new shells), the value returned is always None.

I read a lot and found a relatable answer but it did not solve my problem or rather, I could not understand it.

My bashrc file is:

SLACK_URL="https://hooks.slack.com/"

I am able to see the value in the terminal but not in the Python shell:

ubuntu@ip-A.B.C.D:~$ echo $SLACK_URL 
https://hooks.slack.com/
ubuntu@ip-A.B.C.D:~$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getenv('SLACK_URL')
>>> 

I expect to see the actual variable's value but instead I am getting None.

Aviral Srivastava
  • 4,058
  • 8
  • 29
  • 81

1 Answers1

2

You need to export the variable in your .bashrc file so other programs can see it:

export SLACK_URL="https://hooks.slack.com/"

For more about what export does, see here: https://askubuntu.com/questions/58814/how-do-i-add-environment-variables

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • This is a common duplicate; by having answered it you have now made it that much harder for the OP to delete their question if they so wish, however. – Martijn Pieters Jul 31 '19 at 12:26