Bash – Set persistent environment variable for all users

bashenvironment-variableslinuxpythonUbuntu

I am running Ubuntu on a local PC with the following linux distro/kernel:

$ lsb_release -a
>> ubuntu 16.04.3 LTS

$ uname -r
>> 4.10.0-33-generic

I have a python (3.5) script which calls environment variables via the os package.

For the sake of simplicity, let's use the following script, test_script.py:

import os

MY_VAR = os.environ['MY_VAR']
print(MY_VAR)

When I run this script from terminal:

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

ATTEMPT 1

Reference: [1][4]

$ MY_VAR=123
$ export MY_VAR
$ echo $MY_VAR
>> 123
$ python test_script.py
>> 123

Success! … until I close terminal and reopen terminal. When I do that:

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

ATTEMPT 2

Reference: [2]

To the end of /home/USER/.profile, I add the following lines:

# my variable
MYVAR=123

Save. Confirm saved.

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

ATTEMPT 3

Reference: [2]

To the end of /etc/profile, I add the following lines:

# my variable
MYVAR=123

Save. Confirm saved.

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

ATTEMPT 4

Reference: [2]

Create myvar.sh in /etc/profile.d/

Add the following line:

MYVAR=123

Save. Confirm saved.

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

ATTEMPT 5

Reference: [2][3]

To the end of /etc/environment, I add the following line:

MYVAR=123

Save. Confirm saved.

$ python test_script.py
>>  File "test-script.py", line 3, in <module>
>>    MY_VAR = os.environ['MY_VAR']
>>  File "/home/USER/anaconda3/lib/python3.6/os.py", line 669, in __getitem__
>>    raise KeyError(key) from None
>> KeyError: 'MY_VAR'

Please help! I don't understand what I'm doing wrong here.

[1] How to set environment variables permanently for one user
[2] Permanent Environment Variable for all users
[3] How to permanently set environmental variables
[4] How do I set a user environment variable? (permanently, not session)

Best Answer

You should use the approaches in attempt 3 or 4, but you need to export the variable; change

MYVAR=123

to

export MYVAR=123
Related Question