0% found this document useful (0 votes)
16 views6 pages

Environment Variables - FastAPI

The document explains environment variables in the context of FastAPI and Python, detailing how they can be set and accessed both in the terminal and within Python code. It emphasizes their utility for application configuration and the importance of the PATH variable for locating programs. Additionally, it highlights the need for type conversion and validation when using environment variables in Python.

Uploaded by

manfoya83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Environment Variables - FastAPI

The document explains environment variables in the context of FastAPI and Python, detailing how they can be set and accessed both in the terminal and within Python code. It emphasizes their utility for application configuration and the importance of the PATH variable for locating programs. Additionally, it highlights the need for type conversion and validation when using environment variables in Python.

Uploaded by

manfoya83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

FastAPI Apprendre

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing ↪.

If you already know what "environment variables" are and how to use them, feel free to skip this.

An environment variable (also known as " ") is a variable that lives of the Python code, in the
, and could be read by your Python code (or by other programs as well).

Environment variables could be useful for handling application , as part of the of Python, etc.

You can and use environment variables in the , without needing Python:

bash

You could create an env var MY_NAME with


$ export MY_NAME="Wade Wilson"

Then you could use it with other programs, like


$ echo "Hello $MY_NAME"

Hello Wade Wilson

restart ↻

bash

Create an env var MY_NAME


$ $Env:MY_NAME = "Wade Wilson"

1 of 6 15/09/2025 05:46
Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

Use it with other programs, like


$ echo "Hello $Env:MY_NAME"

Hello Wade Wilson

restart ↻

You could also create environment variables of Python, in the terminal (or with any other method), and then
.

For example you could have a �le main.py with:

import os

name = os.getenv("MY_NAME", "World")


print(f"Hello {name} from Python")

The second argument to os.getenv() [↪] is the default value to return.

If not provided, it's None by default, here we provide "World" as the default value to use.

Then you could call that Python program:

bash

Here we don't set the env var yet


$ python main.py

As we didn't set the env var, we get the default value

Hello World from Python

But if we create an environment variable first


$ export MY_NAME="Wade Wilson"

And then call the program again


$ python main.py

Now it can read the environment variable

Hello Wade Wilson from Python

restart ↻

2 of 6 15/09/2025 05:46
Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

bash

Here we don't set the env var yet


$ python main.py

As we didn't set the env var, we get the default value

Hello World from Python

But if we create an environment variable first


$ $Env:MY_NAME = "Wade Wilson"

And then call the program again


$ python main.py

Now it can read the environment variable

Hello Wade Wilson from Python

restart ↻

As 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 �les, it's common to use them for con�gurations or .

You can also create an environment variable only for a , 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:

bash

Create an env var MY_NAME in line for this program call


$ MY_NAME="Wade Wilson" python main.py

Now it can read the environment variable

Hello Wade Wilson from Python

The env var no longer exists afterwards


$ python main.py

Hello World from Python

restart ↻

3 of 6 15/09/2025 05:46
Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

You can read more about it at The Twelve-Factor App: Con�g [↪].

These environment variables can only handle , 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 that read in Python from an environment variable , 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 handling in the Advanced User Guide -
Settings and Environment Variables ↪.

PATH

There is a environment variable called that is used by the operating systems (Linux, macOS, Windows) to
�nd programs to run.

The 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.

For example, the PATH environment variable could look like this:

∕usr∕local∕bin:∕usr∕bin:∕bin:∕usr∕sbin:∕sbin

This 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\System32

This means that the system should look for programs in the directories:

• C:\Program Files\Python312\Scripts

• C:\Program Files\Python312

• C:\Windows\System32

When you type a in the terminal, the operating system the program in listed
in the PATH environment variable.

For example, when you type python in the terminal, the operating system looks for a program called python in the
in that list.

4 of 6 15/09/2025 05:46
Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

If it �nds it, then it will . Otherwise it keeps looking in the .

Installing Python and Updating the PATH

When you install Python, you might be asked if you want to update the PATH environment variable.

Let's say you install Python and it ends up in a directory ∕opt∕custompython∕bin .

If you say yes to update the PATH environment variable, then the installer will add ∕opt∕custompython∕bin to the PATH
environment variable.

It could look like this:

∕usr∕local∕bin:∕usr∕bin:∕bin:∕usr∕sbin:∕sbin:∕opt∕custompython∕bin

This way, when you type python in the terminal, the system will �nd 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 directory C:\opt\custompython\bin .

If you say yes to update the PATH environment variable, then the installer will add C:\opt\custompython\bin to the PATH
environment variable.

C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:


\opt\custompython\bin

This way, when you type python in the terminal, the system will �nd the Python program in C:\opt\custompython\bin
(the last directory) and use that one.

So, if you type:

bash

The system will the python program in ∕opt∕custompython∕bin and run it.

It would be roughly equivalent to typing:

bash

5 of 6 15/09/2025 05:46
Environment Variables - FastAPI https://fastapi.tiangolo.com/fr/environment-variables/...

The system will the python program in C:\opt\custompython\bin\python and run it.

It would be roughly equivalent to typing:

bash

$ C:\opt\custompython\bin\python

restart ↻

This information will be useful when learning about Virtual Environments ↪.

With this you should have a basic understanding of what are and how to use them in Python.

You can also read more about them in the Wikipedia 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, about Virtual Environments.

6 of 6 15/09/2025 05:46

You might also like