I set an environment variable that I want to access in my Python application. How do I get this value?
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
|
Environment variables are accessed through os.environ
Or you can see a list of all the environment variables using:
As sometimes you might need to see a complete list!
Python default installation on Windows is
|
|||||||||||||||||||||
|
To check if the key exists (returns True/False)
or (removed from python 3.x)
You can also use get when printing the key, useful if you want to use a default. ( for python 2.7.3 )
where /home/username/ is the default |
|||||
|
You can access to the environment variables using
Try to see the content of PYTHONPATH or PYTHONHOME environment variables, maybe this will be helpful for your second question. However you should clarify it. |
||||
|
The original question (first part) was "how to check environment variables in Python." Here's how to check if $FOO is set:
|
|||||
|
As for the environment variables:
I'm afraid you'd have to flesh out your second point a little bit more before a decent answer is possible. |
|||
|