Environment Variables¶
Tip
If you already know what "environment variables" are and how to use them, feel free to skip this.
An environment variable (also known as "env var") is a variable that livesoutside of the Python code, in theoperating system, and could be read by your Python code (or by other programs as well).
Environment variables could be useful for handling applicationsettings, as part of theinstallation of Python, etc.
Create and Use Env Vars¶
You cancreate and use environment variables in theshell (terminal), without needing Python:
// You could create an env var MY_NAME with$exportMY_NAME="Wade Wilson"// Then you could use it with other programs, like$echo"Hello$MY_NAME"Hello Wade Wilson// Create an env var MY_NAME$$Env:MY_NAME="Wade Wilson"// Use it with other programs, like$echo"Hello$Env:MY_NAME"Hello Wade WilsonRead env vars in Python¶
You could also create environment variablesoutside of Python, in the terminal (or with any other method), and thenread them in Python.
For example you could have a filemain.py with:
importosname=os.getenv("MY_NAME","World")print(f"Hello{name} from Python")Tip
The second argument toos.getenv() is the default value to return.
If not provided, it'sNone by default, here we provide"World" as the default value to use.
Then you could call that Python program:
// Here we don't set the env var yet$pythonmain.py// As we didn't set the env var, we get the default valueHello World from Python// But if we create an environment variable first$exportMY_NAME="Wade Wilson"// And then call the program again$pythonmain.py// Now it can read the environment variableHello Wade Wilson from Python// Here we don't set the env var yet$pythonmain.py// As we didn't set the env var, we get the default valueHello World from Python// But if we create an environment variable first$$Env:MY_NAME="Wade Wilson"// And then call the program again$pythonmain.py// Now it can read the environment variableHello Wade Wilson from PythonAs environment variables can be set outside of the code, but can be read by the code, and don't have to be stored (committed togit) with the rest of the files, it's common to use them for configurations orsettings.
You can also create an environment variable only for aspecific program invocation, that is only available to that program, and only for its duration.
To do that, create it right before the program itself, on the same line:
// Create an env var MY_NAME in line for this program call$MY_NAME="Wade Wilson"pythonmain.py// Now it can read the environment variableHello Wade Wilson from Python// The env var no longer exists afterwards$pythonmain.pyHello World from PythonTip
You can read more about it atThe Twelve-Factor App: Config.
Types and Validation¶
These environment variables can only handletext strings, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS).
That means thatany value read in Python from an environment variablewill be astr, and any conversion to a different type or any validation has to be done in code.
You will learn more about using environment variables for handlingapplication settings in theAdvanced User Guide - Settings and Environment Variables.
PATH Environment Variable¶
There is aspecial environment variable calledPATH that is used by the operating systems (Linux, macOS, Windows) to find programs to run.
The value of the variablePATH is a long string that is made of directories separated by a colon: on Linux and macOS, and by a semicolon; on Windows.
For example, thePATH environment variable could look like this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbinThis means that the system should look for programs in the directories:
/usr/local/bin/usr/bin/bin/usr/sbin/sbin
C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32This means that the system should look for programs in the directories:
C:\Program Files\Python312\ScriptsC:\Program Files\Python312C:\Windows\System32
When you type acommand in the terminal, the operating systemlooks for the program ineach of those directories listed in thePATH environment variable.
For example, when you typepython in the terminal, the operating system looks for a program calledpython in thefirst directory in that list.
If it finds it, then it willuse it. Otherwise it keeps looking in theother directories.
Installing Python and Updating thePATH¶
When you install Python, you might be asked if you want to update thePATH environment variable.
Let's say you install Python and it ends up in a directory/opt/custompython/bin.
If you say yes to update thePATH environment variable, then the installer will add/opt/custompython/bin to thePATH environment variable.
It could look like this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/binThis way, when you typepython in the terminal, the system will find the Python program in/opt/custompython/bin (the last directory) and use that one.
Let's say you install Python and it ends up in a directoryC:\opt\custompython\bin.
If you say yes to update thePATH environment variable, then the installer will addC:\opt\custompython\bin to thePATH environment variable.
C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\binThis way, when you typepython in the terminal, the system will find the Python program inC:\opt\custompython\bin (the last directory) and use that one.
So, if you type:
$pythonThe system willfind thepython program in/opt/custompython/bin and run it.
It would be roughly equivalent to typing:
$/opt/custompython/bin/pythonThe system willfind thepython program inC:\opt\custompython\bin\python and run it.
It would be roughly equivalent to typing:
$C:\opt\custompython\bin\pythonThis information will be useful when learning aboutVirtual Environments.
Conclusion¶
With this you should have a basic understanding of whatenvironment variables are and how to use them in Python.
You can also read more about them in theWikipedia for Environment Variable.
In many cases it's not very obvious how environment variables would be useful and applicable right away. But they keep showing up in many different scenarios when you are developing, so it's good to know about them.
For example, you will need this information in the next section, aboutVirtual Environments.







