Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
How can I access the environment variable values in my python application?

1 Answer

0 votes
by (26.4k points)
edited by

Through os.environ, our enivronment variables are accessed

import os

print(os.environ['HOME'])

If you want to see all the environment variables, just use:

os.environ

Want to complete list:

# using get will return `None` if a key is not present rather than raise a `KeyError`

print(os.environ.get('KEY_THAT_MIGHT_EXIST'))

# os.getenv is equivalent, and can also give a default value instead of `None`

print(os.getenv('KEY_THAT_MIGHT_EXIST', default_value))

 For more information, click on this link

Want to know more information about python? Come and join: python online course 

Browse Categories

...