Skip to content

Commit 705659b

Browse files
authored
📝 Add docs about Environment Variables and Virtual Environments (#12054)
1 parent 4f3381a commit 705659b

20 files changed

+1228
-263
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ FastAPI stands on the shoulders of giants:
132132

133133
## Installation
134134

135+
Create and activate a <a href="https://fastapi.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a> and then install FastAPI:
136+
135137
<div class="termy">
136138

137139
```console

docs/en/docs/advanced/settings.md

Lines changed: 4 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,143 +6,25 @@ Most of these settings are variable (can change), like database URLs. And many c
66

77
For this reason it's common to provide them in environment variables that are read by the application.
88

9-
## Environment Variables
10-
11-
/// tip
12-
13-
If you already know what "environment variables" are and how to use them, feel free to skip to the next section below.
14-
15-
///
16-
17-
An <a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">environment variable</a> (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).
18-
19-
You can create and use environment variables in the shell, without needing Python:
20-
21-
//// tab | Linux, macOS, Windows Bash
22-
23-
<div class="termy">
24-
25-
```console
26-
// You could create an env var MY_NAME with
27-
$ export MY_NAME="Wade Wilson"
28-
29-
// Then you could use it with other programs, like
30-
$ echo "Hello $MY_NAME"
31-
32-
Hello Wade Wilson
33-
```
34-
35-
</div>
36-
37-
////
38-
39-
//// tab | Windows PowerShell
40-
41-
<div class="termy">
42-
43-
```console
44-
// Create an env var MY_NAME
45-
$ $Env:MY_NAME = "Wade Wilson"
46-
47-
// Use it with other programs, like
48-
$ echo "Hello $Env:MY_NAME"
49-
50-
Hello Wade Wilson
51-
```
52-
53-
</div>
54-
55-
////
56-
57-
### Read env vars in Python
58-
59-
You could also create environment variables outside of Python, in the terminal (or with any other method), and then read them in Python.
60-
61-
For example you could have a file `main.py` with:
62-
63-
```Python hl_lines="3"
64-
import os
65-
66-
name = os.getenv("MY_NAME", "World")
67-
print(f"Hello {name} from Python")
68-
```
69-
70-
/// tip
71-
72-
The 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.
73-
74-
If not provided, it's `None` by default, here we provide `"World"` as the default value to use.
75-
76-
///
77-
78-
Then you could call that Python program:
79-
80-
<div class="termy">
81-
82-
```console
83-
// Here we don't set the env var yet
84-
$ python main.py
85-
86-
// As we didn't set the env var, we get the default value
87-
88-
Hello World from Python
89-
90-
// But if we create an environment variable first
91-
$ export MY_NAME="Wade Wilson"
92-
93-
// And then call the program again
94-
$ python main.py
95-
96-
// Now it can read the environment variable
97-
98-
Hello Wade Wilson from Python
99-
```
100-
101-
</div>
102-
103-
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 files, it's common to use them for configurations or settings.
104-
105-
You can also create an environment variable only for a specific program invocation, that is only available to that program, and only for its duration.
106-
107-
To do that, create it right before the program itself, on the same line:
108-
109-
<div class="termy">
110-
111-
```console
112-
// Create an env var MY_NAME in line for this program call
113-
$ MY_NAME="Wade Wilson" python main.py
114-
115-
// Now it can read the environment variable
116-
117-
Hello Wade Wilson from Python
118-
119-
// The env var no longer exists afterwards
120-
$ python main.py
121-
122-
Hello World from Python
123-
```
124-
125-
</div>
126-
1279
/// tip
12810

129-
You can read more about it at <a href="https://12factor.net/config" class="external-link" target="_blank">The Twelve-Factor App: Config</a>.
11+
To understand environment variables you can read [Environment Variables](../environment-variables.md){.internal-link target=_blank}.
13012

13113
///
13214

133-
### Types and validation
15+
## Types and validation
13416

13517
These 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).
13618

137-
That means that any value read in Python from an environment variable will be a `str`, and any conversion to a different type or validation has to be done in code.
19+
That 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.
13820

13921
## Pydantic `Settings`
14022

14123
Fortunately, Pydantic provides a great utility to handle these settings coming from environment variables with <a href="https://docs.pydantic.dev/latest/concepts/pydantic_settings/" class="external-link" target="_blank">Pydantic: Settings management</a>.
14224

14325
### Install `pydantic-settings`
14426

145-
First, install the `pydantic-settings` package:
27+
First, make sure you create your [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then install the `pydantic-settings` package:
14628

14729
<div class="termy">
14830

docs/en/docs/advanced/templates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ There are utilities to configure it easily that you can use directly in your **F
88

99
## Install dependencies
1010

11-
Install `jinja2`:
11+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and install `jinja2`:
1212

1313
<div class="termy">
1414

docs/en/docs/advanced/websockets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ You can use <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets
44

55
## Install `WebSockets`
66

7-
First you need to install `WebSockets`:
7+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and install `websockets`:
88

99
<div class="termy">
1010

docs/en/docs/contributing.md

Lines changed: 16 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,117 +6,13 @@ First, you might want to see the basic ways to [help FastAPI and get help](help-
66

77
If you already cloned the <a href="https://github.com/fastapi/fastapi" class="external-link" target="_blank">fastapi repository</a> and you want to deep dive in the code, here are some guidelines to set up your environment.
88

9-
### Virtual environment with `venv`
9+
### Virtual environment
1010

11-
You can create an isolated virtual local environment in a directory using Python's `venv` module. Let's do this in the cloned repository (where the `requirements.txt` is):
12-
13-
<div class="termy">
14-
15-
```console
16-
$ python -m venv env
17-
```
18-
19-
</div>
20-
21-
That will create a directory `./env/` with the Python binaries, and then you will be able to install packages for that local environment.
22-
23-
### Activate the environment
24-
25-
Activate the new environment with:
26-
27-
//// tab | Linux, macOS
28-
29-
<div class="termy">
30-
31-
```console
32-
$ source ./env/bin/activate
33-
```
34-
35-
</div>
36-
37-
////
38-
39-
//// tab | Windows PowerShell
40-
41-
<div class="termy">
42-
43-
```console
44-
$ .\env\Scripts\Activate.ps1
45-
```
46-
47-
</div>
48-
49-
////
50-
51-
//// tab | Windows Bash
52-
53-
Or if you use Bash for Windows (e.g. <a href="https://gitforwindows.org/" class="external-link" target="_blank">Git Bash</a>):
54-
55-
<div class="termy">
56-
57-
```console
58-
$ source ./env/Scripts/activate
59-
```
60-
61-
</div>
62-
63-
////
64-
65-
To check it worked, use:
66-
67-
//// tab | Linux, macOS, Windows Bash
68-
69-
<div class="termy">
70-
71-
```console
72-
$ which pip
73-
74-
some/directory/fastapi/env/bin/pip
75-
```
76-
77-
</div>
78-
79-
////
80-
81-
//// tab | Windows PowerShell
82-
83-
<div class="termy">
84-
85-
```console
86-
$ Get-Command pip
87-
88-
some/directory/fastapi/env/bin/pip
89-
```
90-
91-
</div>
92-
93-
////
94-
95-
If it shows the `pip` binary at `env/bin/pip` then it worked. 🎉
96-
97-
Make sure you have the latest pip version on your local environment to avoid errors on the next steps:
98-
99-
<div class="termy">
100-
101-
```console
102-
$ python -m pip install --upgrade pip
103-
104-
---> 100%
105-
```
106-
107-
</div>
108-
109-
/// tip
110-
111-
Every time you install a new package with `pip` under that environment, activate the environment again.
112-
113-
This makes sure that if you use a terminal program installed by that package, you use the one from your local environment and not any other that could be installed globally.
114-
115-
///
11+
Follow the instructions to create and activate a [virtual environment](virtual-environments.md){.internal-link target=_blank} for the internal code of `fastapi`.
11612

11713
### Install requirements using pip
11814

119-
After activating the environment as described above:
15+
After activating the environment, install the required packages:
12016

12117
<div class="termy">
12218

@@ -160,7 +56,19 @@ $ bash scripts/format.sh
16056

16157
It will also auto-sort all your imports.
16258

163-
For it to sort them correctly, you need to have FastAPI installed locally in your environment, with the command in the section above using `-e`.
59+
## Tests
60+
61+
There is a script that you can run locally to test all the code and generate coverage reports in HTML:
62+
63+
<div class="termy">
64+
65+
```console
66+
$ bash scripts/test-cov-html.sh
67+
```
68+
69+
</div>
70+
71+
This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing.
16472

16573
## Docs
16674

@@ -482,17 +390,3 @@ Serving at: http://127.0.0.1:8008
482390
* Search for such links in the translated document using the regex `#[^# ]`.
483391
* Search in all documents already translated into your language for `your-translated-document.md`. For example VS Code has an option "Edit" -> "Find in Files".
484392
* When translating a document, do not "pre-translate" `#hash-parts` that link to headings in untranslated documents.
485-
486-
## Tests
487-
488-
There is a script that you can run locally to test all the code and generate coverage reports in HTML:
489-
490-
<div class="termy">
491-
492-
```console
493-
$ bash scripts/test-cov-html.sh
494-
```
495-
496-
</div>
497-
498-
This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing.

docs/en/docs/deployment/manually.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ When referring to the remote machine, it's common to call it **server**, but als
8282

8383
When you install FastAPI, it comes with a production server, Uvicorn, and you can start it with the `fastapi run` command.
8484

85-
But you can also install an ASGI server manually:
85+
But you can also install an ASGI server manually.
86+
87+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then you can install the server:
8688

8789
//// tab | Uvicorn
8890

docs/en/docs/deployment/server-workers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ And then the Gunicorn-compatible **Uvicorn worker** class would be in charge of
3939

4040
## Install Gunicorn and Uvicorn
4141

42+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then install `gunicorn`:
43+
4244
<div class="termy">
4345

4446
```console

0 commit comments

Comments
 (0)