-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:

[email protected]:~$ echo $SLACK_URL https://hooks.slack.com/ [email protected]:~$ 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.

1
  • 1
    What's hard to understand about that answer? It literally says: use export when defining the env var. Commented Jul 31, 2019 at 12:23

1 Answer 1

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

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.