Why environmental variable returns none in Linux?

In windows to set a new variable = value we can simply add it to the environmental variable. However, in linux, looks a bit tough. this is the procedure which I followed:

Open Terminal and write:

vim ~/.bashrc 

then press the keyboard "i" and then define the variable as follow:

Test_MyVariable='Tessst'
export Test_MyVariable
PATH=$PATH:Test_MyVariable/bin

then :wq

after $echo I see the variable:

echo $Test_MyVariable 

and it returns:

Tessst

but in my sublime Text it returns None.

import os
new_v= os.environ.get('Test_MyVariable')
print(new_v)

None

I even checked all available os.environ but I can't find my Test_MyVariable. Furthermore, I also tried with ~/.bash_profile and ~/.profile and added variable to them, but the same result. Could please tell me what I am doing wrong?

11

1 Answer

When you define a variable in ~/.bashrc, that variable will be present as soon as ~/.bashrc is "sourced" (read). This only happens when you start a new shell (e.g. when you open a new terminal).

So, if you add the new line to your .bashrc file, you will then need to open a new terminal and run your python script there. Alternatively, you can run source ~/.bashrc to source it into the current shell.

Now, you mention sublime but don't really explain why that's relevant, so I will assume you are running your python script inside the sublime editor somehow. Presumably, it has some sort of shell emulator. If that is the case, then the details of how and where to define the variable will depend on how sublime sets up its shell. Try the following:

  1. Just close the sublime window and then open it again and see if it has now re-read your .bashrc.

  2. Sublime could very well not read .bashrc at all. If you are running sublime from some GUI button, you will probably need to log out and log back in before it manages to re-read the variables.

  3. Try opening a new terminal and running sublime (or whatever the command name for launching sublime from the command line is). Does it see the variable then?

7

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like