environment-variables.md
This file
Path
docs/environment-variables.md
302 lines · 8.3 KiB · Markdown
Last changed by
📝 Remove duplicated line in `environment-variables.md` (#1277)Neirzheibefore adoption46b11ca
History of this fileTake it
The walk stopped after 200 commits: lines older than 5959673 point at it.
📝 Update release notesgithub-actionsbefore adoption5959673
lines 1–302why, one step back1# Environment Variables23Before we jump into **Typer** code, let's cover a bit some of the **basics** that we'll need to understand how to work with Python (and programming) in general. Let's check a bit about **environment variables**.45/// tip67If you already know what "environment variables" are and how to use them, feel free to skip this.89///1011An environment variable (also known as "**env var**") is a variable that lives **outside** of the Python code, in the **operating system**, and could be read by your Python code (or by other programs as well).1213Environment variables could be useful for handling application **settings**, as part of the **installation** of Python, etc.1415## Create and Use Env Vars1617You can **create** and use environment variables in the **shell (terminal)**, without needing Python:1819//// tab | Linux, macOS, Windows Bash2021<div class="termy">2223```console24// You could create an env var MY_NAME with25$ export MY_NAME="Wade Wilson"2627// Then you could use it with other programs, like28$ echo "Hello $MY_NAME"2930Hello Wade Wilson31```3233</div>3435////3637//// tab | Windows PowerShell3839<div class="termy">4041```console42// Create an env var MY_NAME43$ $Env:MY_NAME = "Wade Wilson"4445// Use it with other programs, like46$ echo "Hello $Env:MY_NAME"4748Hello Wade Wilson49```5051</div>5253////5455## Read env vars in Python5657You could also create environment variables **outside** of Python, in the terminal (or with any other method), and then **read them in Python**.5859For example you could have a file `main.py` with:6061```Python hl_lines="3"62import os6364name = os.getenv("MY_NAME", "World")65print(f"Hello {name} from Python")66```6768/// tip6970The second argument to <a href="https://docs.python.org/3.8/library/os.html#os.getenv" class="external-link" target="_blank">`os.getenv()`</a> is the default value to return.7172If not provided, it's `None` by default, here we provide `"World"` as the default value to use.7374///7576Then you could call that Python program:7778//// tab | Linux, macOS, Windows Bash7980<div class="termy">8182```console83// Here we don't set the env var yet84$ python main.py8586// As we didn't set the env var, we get the default value8788Hello World from Python8990// But if we create an environment variable first91$ export MY_NAME="Wade Wilson"9293// And then call the program again94$ python main.py9596// Now it can read the environment variable9798Hello Wade Wilson from Python99```100101</div>102103////104105//// tab | Windows PowerShell106107<div class="termy">108109```console110// Here we don't set the env var yet111$ python main.py112113// As we didn't set the env var, we get the default value114115Hello World from Python116117// But if we create an environment variable first118$ $Env:MY_NAME = "Wade Wilson"119120// And then call the program again121$ python main.py122123// Now it can read the environment variable124125Hello Wade Wilson from Python126```127128</div>129130////131132As environment variables can be set outside of the code, but can be read by the code, and don't have to be stored (committed to `git`) with the rest of the files, it's common to use them for configurations or **settings**.133134You can also create an environment variable only for a **specific program invocation**, that is only available to that program, and only for its duration.135136To do that, create it right before the program itself, on the same line:137138<div class="termy">139140```console141// Create an env var MY_NAME in line for this program call142$ MY_NAME="Wade Wilson" python main.py143144// Now it can read the environment variable145146Hello Wade Wilson from Python147148// The env var no longer exists afterwards149$ python main.py150151Hello World from Python152```153154</div>155156/// tip157158You can read more about it at <a href="https://12factor.net/config" class="external-link" target="_blank">The Twelve-Factor App: Config</a>.159160///161162## Types and Validation163164These environment variables can only handle **text 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).165166That means that **any value** read in Python from an environment variable **will be a `str`**, and any conversion to a different type or any validation has to be done in code.167168You will learn more about using environment variables for your <abbr title="command line interface">CLI</abbr> applications later in the section about [CLI Arguments with Environment Variables](./tutorial/arguments/envvar.md){.internal-link target=_blank}.169170## `PATH` Environment Variable171172There is a **special** environment variable called **`PATH`** that is used by the operating systems (Linux, macOS, Windows) to find programs to run.173174The value of the variable `PATH` is a long string that is made of directories separated by a colon `:` on Linux and macOS, and by a semicolon `;` on Windows.175176For example, the `PATH` environment variable could look like this:177178//// tab | Linux, macOS179180```plaintext181/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin182```183184This means that the system should look for programs in the directories:185186* `/usr/local/bin`187* `/usr/bin`188* `/bin`189* `/usr/sbin`190* `/sbin`191192////193194//// tab | Windows195196```plaintext197C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32198```199200This means that the system should look for programs in the directories:201202* `C:\Program Files\Python312\Scripts`203* `C:\Program Files\Python312`204* `C:\Windows\System32`205206////207208When you type a **command** in the terminal, the operating system **looks for** the program in **each of those directories** listed in the `PATH` environment variable.209210For example, when you type `python` in the terminal, the operating system looks for a program called `python` in the **first directory** in that list.211212If it finds it, then it will **use it**. Otherwise it keeps looking in the **other directories**.213214### Installing Python and Updating the `PATH`215216When you install Python, you might be asked if you want to update the `PATH` environment variable.217218//// tab | Linux, macOS219220Let's say you install Python and it ends up in a directory `/opt/custompython/bin`.221222If you say yes to update the `PATH` environment variable, then the installer will add `/opt/custompython/bin` to the `PATH` environment variable.223224It could look like this:225226```plaintext227/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/bin228```229230This way, when you type `python` in the terminal, the system will find the Python program in `/opt/custompython/bin` (the last directory) and use that one.231232////233234//// tab | Windows235236Let's say you install Python and it ends up in a directory `C:\opt\custompython\bin`.237238If you say yes to update the `PATH` environment variable, then the installer will add `C:\opt\custompython\bin` to the `PATH` environment variable.239240```plaintext241C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\bin242```243244This way, when you type `python` in the terminal, the system will find the Python program in `C:\opt\custompython\bin` (the last directory) and use that one.245246////247248So, if you type:249250<div class="termy">251252```console253$ python254```255256</div>257258//// tab | Linux, macOS259260The system will **find** the `python` program in `/opt/custompython/bin` and run it.261262It would be roughly equivalent to typing:263264<div class="termy">265266```console267$ /opt/custompython/bin/python268```269270</div>271272////273274//// tab | Windows275276The system will **find** the `python` program in `C:\opt\custompython\bin\python` and run it.277278It would be roughly equivalent to typing:279280<div class="termy">281282```console283$ C:\opt\custompython\bin\python284```285286</div>287288////289290This information will be useful when learning about [Virtual Environments](virtual-environments.md){.internal-link target=_blank}.291292It will also be useful when you **create your own CLI programs** as, for them to be available for your users, they will need to be somewhere in the `PATH` environment variable.293294## Conclusion295296With this you should have a basic understanding of what **environment variables** are and how to use them in Python.297298You can also read more about them in the <a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">Wikipedia for Environment Variable</a>.299300In 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.301302For example, you will need this information in the next section, about [Virtual Environments](virtual-environments.md).