Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k 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 (106k 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:-

Browse Categories

...