You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -132,6 +132,8 @@ FastAPI stands on the shoulders of giants:
132
132
133
133
## Installation
134
134
135
+
Create and activate a <ahref="https://fastapi.tiangolo.com/virtual-environments/"class="external-link"target="_blank">virtual environment</a> and then install FastAPI:
Copy file name to clipboardExpand all lines: docs/en/docs/advanced/settings.md
+4-122Lines changed: 4 additions & 122 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,143 +6,25 @@ Most of these settings are variable (can change), like database URLs. And many c
6
6
7
7
For this reason it's common to provide them in environment variables that are read by the application.
8
8
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 <ahref="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
-
<divclass="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
-
<divclass="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 <ahref="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
-
<divclass="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
-
<divclass="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
-
127
9
/// tip
128
10
129
-
You can read more about it at <ahref="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}.
130
12
131
13
///
132
14
133
-
###Types and validation
15
+
## Types and validation
134
16
135
17
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).
136
18
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.
138
20
139
21
## Pydantic `Settings`
140
22
141
23
Fortunately, Pydantic provides a great utility to handle these settings coming from environment variables with <ahref="https://docs.pydantic.dev/latest/concepts/pydantic_settings/"class="external-link"target="_blank">Pydantic: Settings management</a>.
142
24
143
25
### Install `pydantic-settings`
144
26
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:
@@ -6,117 +6,13 @@ First, you might want to see the basic ways to [help FastAPI and get help](help-
6
6
7
7
If you already cloned the <ahref="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.
8
8
9
-
### Virtual environment with `venv`
9
+
### Virtual environment
10
10
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
-
<divclass="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
-
<divclass="termy">
30
-
31
-
```console
32
-
$ source ./env/bin/activate
33
-
```
34
-
35
-
</div>
36
-
37
-
////
38
-
39
-
//// tab | Windows PowerShell
40
-
41
-
<divclass="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. <ahref="https://gitforwindows.org/"class="external-link"target="_blank">Git Bash</a>):
54
-
55
-
<divclass="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
-
<divclass="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
-
<divclass="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
-
<divclass="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`.
116
12
117
13
### Install requirements using pip
118
14
119
-
After activating the environment as described above:
15
+
After activating the environment, install the required packages:
120
16
121
17
<divclass="termy">
122
18
@@ -160,7 +56,19 @@ $ bash scripts/format.sh
160
56
161
57
It will also auto-sort all your imports.
162
58
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
+
<divclass="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.
* Search for such links in the translated document using the regex `#[^# ]`.
483
391
* 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".
484
392
* 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.
Copy file name to clipboardExpand all lines: docs/en/docs/deployment/manually.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,9 @@ When referring to the remote machine, it's common to call it **server**, but als
82
82
83
83
When you install FastAPI, it comes with a production server, Uvicorn, and you can start it with the `fastapi run` command.
84
84
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:
0 commit comments