41

I tried this:

os.environ['MyVar']

But it did not work! Is there any way suitable for all operating systems?

5
  • 2
    I got this error: KeyError: 'MyVar' Commented May 8, 2012 at 10:23
  • 2
    Are you sure that you defined variable in a right way? Commented May 8, 2012 at 10:38
  • 2
    How to define it? I have add it in Control Panel->System and Security -> System -> Advamced system settings->Environmental Variables Commented May 8, 2012 at 10:59
  • You can check the variable in the console using the command SET. Commented May 8, 2012 at 11:17
  • 3
    Did you set the environment variable after starting the shell you're running Python from? Commented May 8, 2012 at 11:18

3 Answers 3

65

Try using the following:

os.getenv('MyVar')

From the documentation:

os.getenv(varname[, value])

Return the value of the environment variable varname if it exists, or value if it doesn’t. value defaults to None.

Availability: most flavors of Unix, Windows

So after testing it:

>>> import os
>>> os.environ['MyVar'] = 'Hello World!'       # set the environment variable 'MyVar' to contain 'Hello World!'
>>> print os.getenv('MyVar')
Hello World!
>>> print os.getenv('not_existing_variable')
None
>>> print os.getenv('not_existing_variable', 'that variable does not exist')
that variable does not exist
>>> print os.environ['MyVar']
Hello World!
>>> print os.environ['not_existing_variable']
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/UserDict.py", line 17, in __getitem__
    def __getitem__(self, key): return self.data[key]
KeyError: 'not_existing_variable    

Your method would work too if the environmental variable exists. The difference with using os.getenv is that it returns None (or the given value), while os.environ['MyValue'] gives a KeyError exception when the variable does not exist.

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

1 Comment

Whatever the combination is, by default it is printing out None.
28

You might have to restart windows to be able to read the environment variable that you set through the control panel.

5 Comments

Or just restarting the console.
This worked! I tried just restarting Spyder and Jupyter Notebook, and neither could find the environment variables I needed. Once I restarted my computer, the os.getenv(wanted_env_var) function worked perfectly. Thanks!
Had to restart machine, restarting console didn't cut it +1
What is console?
@BetterCallMe In this context, it could be a command prompt, PowerShell, Terminal, or any development environment being used. Usually, you will need to restart these for changes made to the System Environment Variables to reflect.
6
os.getenv('PATH')

You can test it with the above line of code. It will list all the paths which are set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.