Skip to content

Commit a25c92c

Browse files
authored
♻️ Add support for pip install "fastapi[standard]" with standard dependencies and python -m fastapi (fastapi#11935)
* ♻️ Add support for `pip install "fastapi[standard]"` and make `fastapi` not include the optional standard dependencies * 📝 Update docs to include new fastapi[standard] * ✨ Add new stub fastapi command that tells people to install fastapi[standard] * ✅ Add tests for new stub CLI * 🔧 Add new command fastapi in main fastapi project, for when fastapi-cli is not installed * ✏️ Fix types * 📝 Add note about quotes when installing fastapi[standard] * 📝 Update docs about standard extra dependencies * ⬆️ Upgrade fastapi-cli
1 parent 3990a0a commit a25c92c

File tree

15 files changed

+114
-113
lines changed

15 files changed

+114
-113
lines changed

README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@ FastAPI stands on the shoulders of giants:
135135
<div class="termy">
136136

137137
```console
138-
$ pip install fastapi
138+
$ pip install "fastapi[standard]"
139139

140140
---> 100%
141141
```
142142

143143
</div>
144144

145+
**Note**: Make sure you put `"fastapi[standard]"` in quotes to ensure it works in all terminals.
146+
145147
## Example
146148

147149
### Create it
@@ -452,11 +454,15 @@ To understand more about it, see the section <a href="https://fastapi.tiangolo.c
452454

453455
## Dependencies
454456

457+
FastAPI depends on Pydantic and Starlette.
458+
459+
### `standard` Dependencies
460+
461+
When you install FastAPI with `pip install "fastapi[standard]"` it comes the `standard` group of optional dependencies:
462+
455463
Used by Pydantic:
456464

457465
* <a href="https://github.com/JoshData/python-email-validator" target="_blank"><code>email_validator</code></a> - for email validation.
458-
* <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" target="_blank"><code>pydantic-settings</code></a> - for settings management.
459-
* <a href="https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/" target="_blank"><code>pydantic-extra-types</code></a> - for extra types to be used with Pydantic.
460466

461467
Used by Starlette:
462468

@@ -466,33 +472,26 @@ Used by Starlette:
466472

467473
Used by FastAPI / Starlette:
468474

469-
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application.
475+
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application. This includes `uvicorn[standard]`, which includes some dependencies (e.g. `uvloop`) needed for high performance serving.
470476
* `fastapi-cli` - to provide the `fastapi` command.
471477

472-
When you install `fastapi` it comes these standard dependencies.
473-
474-
Additional optional dependencies:
475-
476-
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`.
477-
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
478+
### Without `standard` Dependencies
478479

479-
## `fastapi-slim`
480+
If you don't want to include the `standard` optional dependencies, you can install with `pip install fastapi` instead of `pip install "fastapi[standard]"`.
480481

481-
If you don't want the extra standard optional dependencies, install `fastapi-slim` instead.
482+
### Additional Optional Dependencies
482483

483-
When you install with:
484+
There are some additional dependencies you might want to install.
484485

485-
```bash
486-
pip install fastapi
487-
```
486+
Additional optional Pydantic dependencies:
488487

489-
...it includes the same code and dependencies as:
488+
* <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" target="_blank"><code>pydantic-settings</code></a> - for settings management.
489+
* <a href="https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/" target="_blank"><code>pydantic-extra-types</code></a> - for extra types to be used with Pydantic.
490490

491-
```bash
492-
pip install "fastapi-slim[standard]"
493-
```
491+
Additional optional FastAPI dependencies:
494492

495-
The standard extra dependencies are the ones mentioned above.
493+
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`.
494+
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
496495

497496
## License
498497

docs/em/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ FastAPI 🧍 🔛 ⌚ 🐘:
133133
<div class="termy">
134134

135135
```console
136-
$ pip install fastapi
136+
$ pip install "fastapi[standard]"
137137

138138
---> 100%
139139
```

docs/em/docs/tutorial/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $ pip install "fastapi[all]"
5858
👉 ⚫️❔ 👆 🔜 🎲 🕐 👆 💚 🛠️ 👆 🈸 🏭:
5959

6060
```
61-
pip install fastapi
61+
pip install "fastapi[standard]"
6262
```
6363

6464
❎ `uvicorn` 👷 💽:

docs/en/docs/deployment/docker.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ You would of course use the same ideas you read in [About FastAPI versions](vers
113113
For example, your `requirements.txt` could look like:
114114

115115
```
116-
fastapi>=0.112.0,<0.113.0
116+
fastapi[standard]>=0.113.0,<0.114.0
117117
pydantic>=2.7.0,<3.0.0
118118
```
119119

docs/en/docs/deployment/manually.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ But you can also install an ASGI server manually:
103103

104104
That including `uvloop`, the high-performance drop-in replacement for `asyncio`, that provides the big concurrency performance boost.
105105

106-
When you install FastAPI with something like `pip install fastapi` you already get `uvicorn[standard]` as well.
106+
When you install FastAPI with something like `pip install "fastapi[standard]"` you already get `uvicorn[standard]` as well.
107107

108108
=== "Hypercorn"
109109

docs/en/docs/deployment/versions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ You can create production applications with **FastAPI** right now (and you have
1212

1313
The first thing you should do is to "pin" the version of **FastAPI** you are using to the specific latest version that you know works correctly for your application.
1414

15-
For example, let's say you are using version `0.45.0` in your app.
15+
For example, let's say you are using version `0.112.0` in your app.
1616

1717
If you use a `requirements.txt` file you could specify the version with:
1818

1919
```txt
20-
fastapi==0.45.0
20+
fastapi[standard]==0.112.0
2121
```
2222

23-
that would mean that you would use exactly the version `0.45.0`.
23+
that would mean that you would use exactly the version `0.112.0`.
2424

2525
Or you could also pin it with:
2626

2727
```txt
28-
fastapi>=0.45.0,<0.46.0
28+
fastapi[standard]>=0.112.0,<0.113.0
2929
```
3030

31-
that would mean that you would use the versions `0.45.0` or above, but less than `0.46.0`, for example, a version `0.45.2` would still be accepted.
31+
that would mean that you would use the versions `0.112.0` or above, but less than `0.113.0`, for example, a version `0.112.2` would still be accepted.
3232

3333
If you use any other tool to manage your installations, like Poetry, Pipenv, or others, they all have a way that you can use to define specific versions for your packages.
3434

@@ -78,10 +78,10 @@ So, you can just let **FastAPI** use the correct Starlette version.
7878

7979
Pydantic includes the tests for **FastAPI** with its own tests, so new versions of Pydantic (above `1.0.0`) are always compatible with FastAPI.
8080

81-
You can pin Pydantic to any version above `1.0.0` that works for you and below `2.0.0`.
81+
You can pin Pydantic to any version above `1.0.0` that works for you.
8282

8383
For example:
8484

8585
```txt
86-
pydantic>=1.2.0,<2.0.0
86+
pydantic>=2.7.0,<3.0.0
8787
```

docs/en/docs/fastapi-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
**FastAPI CLI** is a command line program that you can use to serve your FastAPI app, manage your FastAPI project, and more.
44

5-
When you install FastAPI (e.g. with `pip install fastapi`), it includes a package called `fastapi-cli`, this package provides the `fastapi` command in the terminal.
5+
When you install FastAPI (e.g. with `pip install "fastapi[standard]"`), it includes a package called `fastapi-cli`, this package provides the `fastapi` command in the terminal.
66

77
To run your FastAPI app for development, you can use the `fastapi dev` command:
88

docs/en/docs/index.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ FastAPI stands on the shoulders of giants:
131131
<div class="termy">
132132

133133
```console
134-
$ pip install fastapi
134+
$ pip install "fastapi[standard]"
135135

136136
---> 100%
137137
```
138138

139139
</div>
140140

141+
**Note**: Make sure you put `"fastapi[standard]"` in quotes to ensure it works in all terminals.
142+
141143
## Example
142144

143145
### Create it
@@ -448,11 +450,15 @@ To understand more about it, see the section <a href="https://fastapi.tiangolo.c
448450

449451
## Dependencies
450452

453+
FastAPI depends on Pydantic and Starlette.
454+
455+
### `standard` Dependencies
456+
457+
When you install FastAPI with `pip install "fastapi[standard]"` it comes the `standard` group of optional dependencies:
458+
451459
Used by Pydantic:
452460

453461
* <a href="https://github.com/JoshData/python-email-validator" target="_blank"><code>email_validator</code></a> - for email validation.
454-
* <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" target="_blank"><code>pydantic-settings</code></a> - for settings management.
455-
* <a href="https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/" target="_blank"><code>pydantic-extra-types</code></a> - for extra types to be used with Pydantic.
456462

457463
Used by Starlette:
458464

@@ -462,33 +468,26 @@ Used by Starlette:
462468

463469
Used by FastAPI / Starlette:
464470

465-
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application.
471+
* <a href="https://www.uvicorn.org" target="_blank"><code>uvicorn</code></a> - for the server that loads and serves your application. This includes `uvicorn[standard]`, which includes some dependencies (e.g. `uvloop`) needed for high performance serving.
466472
* `fastapi-cli` - to provide the `fastapi` command.
467473

468-
When you install `fastapi` it comes these standard dependencies.
469-
470-
Additional optional dependencies:
471-
472-
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`.
473-
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
474+
### Without `standard` Dependencies
474475

475-
## `fastapi-slim`
476+
If you don't want to include the `standard` optional dependencies, you can install with `pip install fastapi` instead of `pip install "fastapi[standard]"`.
476477

477-
If you don't want the extra standard optional dependencies, install `fastapi-slim` instead.
478+
### Additional Optional Dependencies
478479

479-
When you install with:
480+
There are some additional dependencies you might want to install.
480481

481-
```bash
482-
pip install fastapi
483-
```
482+
Additional optional Pydantic dependencies:
484483

485-
...it includes the same code and dependencies as:
484+
* <a href="https://docs.pydantic.dev/latest/usage/pydantic_settings/" target="_blank"><code>pydantic-settings</code></a> - for settings management.
485+
* <a href="https://docs.pydantic.dev/latest/usage/types/extra_types/extra_types/" target="_blank"><code>pydantic-extra-types</code></a> - for extra types to be used with Pydantic.
486486

487-
```bash
488-
pip install "fastapi-slim[standard]"
489-
```
487+
Additional optional FastAPI dependencies:
490488

491-
The standard extra dependencies are the ones mentioned above.
489+
* <a href="https://github.com/ijl/orjson" target="_blank"><code>orjson</code></a> - Required if you want to use `ORJSONResponse`.
490+
* <a href="https://github.com/esnme/ultrajson" target="_blank"><code>ujson</code></a> - Required if you want to use `UJSONResponse`.
492491

493492
## License
494493

docs/en/docs/tutorial/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ The first step is to install FastAPI:
7676
<div class="termy">
7777

7878
```console
79-
$ pip install fastapi
79+
$ pip install "fastapi[standard]"
8080

8181
---> 100%
8282
```
8383

8484
</div>
8585

8686
!!! note
87-
When you install with `pip install fastapi` it comes with some default optional standard dependencies.
87+
When you install with `pip install "fastapi[standard]"` it comes with some default optional standard dependencies.
8888

89-
If you don't want to have those optional dependencies, you can instead install `pip install fastapi-slim`.
89+
If you don't want to have those optional dependencies, you can instead install `pip install fastapi`.
9090

9191
## Advanced User Guide
9292

docs/en/docs/tutorial/security/first-steps.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ Copy the example in a file `main.py`:
4545
## Run it
4646

4747
!!! info
48-
The <a href="https://github.com/Kludex/python-multipart" class="external-link" target="_blank">`python-multipart`</a> package is automatically installed with **FastAPI** when you run the `pip install fastapi` command.
48+
The <a href="https://github.com/Kludex/python-multipart" class="external-link" target="_blank">`python-multipart`</a> package is automatically installed with **FastAPI** when you run the `pip install "fastapi[standard]"` command.
4949

50-
However, if you use the `pip install fastapi-slim` command, the `python-multipart` package is not included by default. To install it manually, use the following command:
50+
However, if you use the `pip install fastapi` command, the `python-multipart` package is not included by default. To install it manually, use the following command:
5151

5252
`pip install python-multipart`
5353

fastapi/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from fastapi.cli import main
2+
3+
main()

fastapi/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
try:
2+
from fastapi_cli.cli import main as cli_main
3+
4+
except ImportError: # pragma: no cover
5+
cli_main = None # type: ignore
6+
7+
8+
def main() -> None:
9+
if not cli_main: # type: ignore[truthy-function]
10+
message = 'To use the fastapi command, please install "fastapi[standard]":\n\n\tpip install "fastapi[standard]"\n'
11+
print(message)
12+
raise RuntimeError(message) # noqa: B904
13+
cli_main()

pdm_build.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Any, Dict, List
2+
from typing import Any, Dict
33

44
from pdm.backend.hooks import Context
55

@@ -11,29 +11,10 @@ def pdm_build_initialize(context: Context) -> None:
1111
# Get custom config for the current package, from the env var
1212
config: Dict[str, Any] = context.config.data["tool"]["tiangolo"][
1313
"_internal-slim-build"
14-
]["packages"][TIANGOLO_BUILD_PACKAGE]
14+
]["packages"].get(TIANGOLO_BUILD_PACKAGE)
15+
if not config:
16+
return
1517
project_config: Dict[str, Any] = config["project"]
16-
# Get main optional dependencies, extras
17-
optional_dependencies: Dict[str, List[str]] = metadata.get(
18-
"optional-dependencies", {}
19-
)
20-
# Get custom optional dependencies name to always include in this (non-slim) package
21-
include_optional_dependencies: List[str] = config.get(
22-
"include-optional-dependencies", []
23-
)
2418
# Override main [project] configs with custom configs for this package
2519
for key, value in project_config.items():
2620
metadata[key] = value
27-
# Get custom build config for the current package
28-
build_config: Dict[str, Any] = (
29-
config.get("tool", {}).get("pdm", {}).get("build", {})
30-
)
31-
# Override PDM build config with custom build config for this package
32-
for key, value in build_config.items():
33-
context.config.build_config[key] = value
34-
# Get main dependencies
35-
dependencies: List[str] = metadata.get("dependencies", [])
36-
# Add optional dependencies to the default dependencies for this (non-slim) package
37-
for include_optional in include_optional_dependencies:
38-
optional_dependencies_group = optional_dependencies.get(include_optional, [])
39-
dependencies.extend(optional_dependencies_group)

0 commit comments

Comments
 (0)