0 votes
1 view
in Python by (47.8k points)

I set an environment variable that I want to access in my Python application. How do I get this value?

1 Answer

0 votes
by (107k points)
edited by
  • We can access the list of environment variables through os.environ.

   import os

   print(os.environ['HOME'])

 image

  • Following is the piece of code only for Windows to find the environment variable:- 

import sys 

print(sys.prefix)

  • You can use the following code to check the environment variable in Python on all the Os:-

try: 

  os.environ["FOO"] 

Except KeyError:

   print ("Please set the environment variable FOO")

   sys.exit(1)

To know more about this you can have a look at the following video tutorial:-

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...