7

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)

1 Answer 1

12

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

MYVAR=123

to

export MYVAR=123
6
  • Please, write the whole solution, it'll be more useful. Aug 5, 2018 at 14:20
  • This solution did NOT work for me on Ubuntu 18.04. I attempted to create MYVAR=123 even with the export keyword before it, both in /etc/profile and /etc/profile.d/myvar.sh however when I echo $MYVAR it is empty.
    – ipkpjersi
    Aug 29, 2019 at 13:39
  • 1
    @ipkpjersi are you using bash? Are you starting it as a login shell? Aug 29, 2019 at 14:11
  • I tried with Bash and Zsh. Zsh is my login shell.
    – ipkpjersi
    Aug 29, 2019 at 14:23
  • 1
    It doesn’t matter what your login shell is; what matters is whether you’re starting the shell as a login shell. That depends on how you’re starting it: are you starting it from a terminal emulator? If so, which one? Aug 29, 2019 at 14:32

You must log in to answer this question.

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