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: package-structure-code/declare-dependencies.md
+64-64Lines changed: 64 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,13 @@
3
3
:og:description: A Python package dependency refers to an external package or software that your Python project requires to function properly. Learn how to add different types of dependencies to your Python package.
4
4
```
5
5
6
-
7
6
# Dependencies for your Python Package
8
7
9
-
10
8
In the [pyproject.toml overview page](pyproject-toml-python-package-metadata),
11
9
you learned how to set up a **pyproject.toml** file with basic metadata
12
10
for your package. On this page, you will learn how to specify different types of
13
11
dependencies in your `pyproject.toml`.
14
12
15
-
16
-
17
13
## What is a package dependency?
18
14
19
15
A Python package dependency refers to an external package or
@@ -58,13 +54,11 @@ tests = [
58
54
59
55
There are three different types of dependencies that you will learn about on this page:
60
56
61
-
1.**Required dependencies:** These are dependencies that need to be installed for your package to work correctly in a user's environment. You add these dependencies to the `project.dependencies` table in your pyproject.toml file.
62
-
2.**Feature Dependencies:** These are dependencies that are required if a user wants to access additional functionality (that is not core) to your package.
63
-
3.**Development Dependencies:** These dependencies are required is someone wants to develop or work on your package. For instance linters, testing tools like pytest and mypy are examples of development dependencies.
57
+
1.**Required dependencies:** These are dependencies that need to be installed for your package to work correctly in a user's environment. You add these dependencies to the `[project.dependencies]` table in your pyproject.toml file.
58
+
2.**Feature Dependencies:** These are dependencies that are required if a user wants to access additional functionality (that is not core) to your package. Store these in the `[project.optional.dependencies]` table or your pyproject.toml file.
59
+
3.**Development Dependencies:** These dependencies are required if someone wants to develop or work on your package. These include instance linters, testing tools like pytest and mypy are examples of development dependencies. Store these in the `[project.dependency.groups]` table or your pyproject.toml file.
64
60
65
61
:::{admonition}
66
-
:class: tip
67
-
68
62
A dependency is not part of your project's codebase. It is a package or software called
69
63
within the code of your project or used during the development of your package.
70
64
:::
@@ -74,10 +68,10 @@ within the code of your project or used during the development of your package.
74
68
Required dependencies are imported and called directly within your package's code.
75
69
They are needed for your package to run.
76
70
77
-
You can add your core dependencies to the `dependencies` array in the
71
+
You can add your required dependencies to the `dependencies` array in the
78
72
`[project]` table of your **pyproject.toml** file. When users install
79
73
your package with uv, pip, or conda, these dependencies will be
80
-
automatically installed alongside your package.
74
+
automatically installed alongside your package in their environment.
81
75
82
76
```toml
83
77
[project]
@@ -126,22 +120,22 @@ dependencies = [
126
120
:class: tip
127
121
128
122
If you have dependencies that need to be installed directly from GitHub,
129
-
you can specify them in your pyproject.toml file:
123
+
you can specify them in your pyproject.toml file like this:
IMPORTANT: For security reasons, if your library depends on a
137
-
GitHub-hosted project, you will need to point to a specific
138
-
commit/tag/hash of that repository in order to upload your project to
139
-
PyPI.
130
+
IMPORTANT: If your library depends on a GitHub-hosted project,
131
+
you should point to a specific commit/tag/hash of that repository before you upload your project to
132
+
PyPI. You never know how the project might change over time. Commit hashes
133
+
are more reliable as they can't be changed
140
134
:::
141
135
142
136
## 2. Optional dependencies
143
137
144
-
Optional (sometimes referred to as feature) dependencies can be installed by users as needed. Optional dependencies add specific features to your package that not all users need. For example, if your package has an optional interactive plotting feature that uses Bokeh, you would list Bokeh as an `[optional.dependency]`. Users who want interactive plotting will install it. Users who don't need plotting don't have to install it.
138
+
Optional (also referred to as feature) dependencies can be installed by users as needed. Optional dependencies add specific features to your package that not all users need. For example, if your package has an optional interactive plotting feature that uses Bokeh, you would list Bokeh as an `[optional.dependency]`. Users who want interactive plotting will install it. Users who don't need plotting don't have to install it.
145
139
146
140
Place these dependencies in the `[project.optional-dependencies]` table.
147
141
@@ -150,15 +144,12 @@ Place these dependencies in the `[project.optional-dependencies]` table.
150
144
...
151
145
...
152
146
...
153
-
# Below you see a optional. A dependency array called plot that lists packages a user needs to access the plotting functionality, which is a feature of your project.
154
147
[optional.dependencies]
155
148
plot = ["bokeh"]
156
149
```
157
150
158
151
When a user installs your package, uv, pip, or conda automatically installs all required dependencies. Optional dependencies are only installed if the user explicitly requests them.
159
152
160
-
161
-
162
153
:::{dropdown} How to Add optional.dependencies using UV
163
154
:icon: eye
164
155
:color: primary
@@ -273,7 +264,7 @@ dependencies for development work or additional features.
273
264
274
265
### Install dependency groups
275
266
276
-
When users install your package, only core dependencies are installed by
267
+
When someone installs your package, only core dependencies are installed by
277
268
default. To install optional dependencies, you
278
269
need to specify which groups to include when installing the package.
279
270
@@ -288,97 +279,102 @@ core dependencies, and the test dependencies from the
288
279
`[project.optional-dependencies]` table.
289
280
:::
290
281
291
-
### Using uv
282
+
### Using uv or pip for installation
292
283
293
284
UV streamlines this process, allowing you to sync a venv in your project directory
294
285
with both an editable install of your package and its dependencies automatically.
286
+
You can also use pip and install dependencies into the environment of your choice.
295
287
296
288
:::{todo}
297
289
We shouldn't show UV pip install, so how do you add optional feature deps with UV??
298
290
:::
299
291
300
-
**Install optional dependencies:**
301
-
```bash
302
-
# FIXME
303
-
uv pip install -e ".[docs]"# Single group
304
-
uv pip install -e ".[docs,tests,lint]"# Multiple groups
305
-
```
306
292
307
293
**Install development groups:**
308
294
309
-
You can use uv sync to sync dependency groups too
310
-
```bash
311
-
# TEST ME
312
-
uv sync --group docs # Single group
313
-
uv sync --group docs --group test# Multiple groups
314
-
uv sync --all-groups # All development groups
295
+
:::::{tab-set}
296
+
297
+
::::{tab-item} Use UV
298
+
You can use uv sync to sync dependency groups in your uv-managed venv
299
+
300
+
```console
301
+
$ uv sync --group docs # Single group
302
+
$ uv sync --group docs --group test# Multiple groups
303
+
$ uv sync --all-groups # All development groups
315
304
```
316
305
317
-
**Install everything (package + all dependencies):**
318
-
```bash
319
-
uv sync --all-extras --all-groups
306
+
**Install optional dependencies:**
307
+
308
+
```console
309
+
# uv pip install is not idea if you are using uv supported venvs for your project
310
+
$ uv pip install -e ".[docs]"# Single group
311
+
$ uv pip install -e ".[docs,tests,lint]"# Multiple groups
320
312
```
321
313
322
-
:::{admonition} uv sync vs uv pip install
323
-
:class: tip
314
+
**Install everything (package + all dependencies):**
315
+
316
+
```console
317
+
$ uv sync --all-extras --all-groups
318
+
```
324
319
325
320
`uv sync` is the recommended command for development workflows. It
326
321
manages your virtual environment and keeps your lockfile up to date.
327
322
Use `uv pip install` when you need pip-compatible behavior.
328
-
:::
323
+
::::
329
324
330
-
### Using pip
325
+
::::{tab-item} Use pip (version >=25.1)
331
326
332
327
**Install optional dependencies:**
333
-
```bash
328
+
329
+
```console
334
330
python -m pip install -e ".[docs]" # Single group
335
331
python -m pip install -e ".[docs,tests,lint]" # Multiple groups
336
332
```
333
+
**Install dependency groups:**
337
334
338
-
:::{admonition} Using `python -m pip install` vs. `pip install`
335
+
```console
336
+
python -m pip install --group test # Single group
337
+
python -m pip install --group docs # Multiple groups
338
+
```
339
339
340
-
We recommend calling pip using `python -m pip` to ensure you're using
340
+
Always call pip using `python -m pip` to ensure you're using
341
341
the pip from your current active Python environment. This helps avoid
342
342
installation conflicts.
343
-
:::
344
-
345
-
346
-
:::{admonition} For zsh shell users
347
-
:class: tip
348
343
349
-
Some shells (like zsh on Mac and certain Windows shells) require quotes
350
-
around brackets:
344
+
**Note:** Some shells (like zsh on Mac) require quotes around brackets to run successfully:
351
345
352
346
`python -m pip install ".[tests]"`
353
347
354
-
Without quotes, the command may fail in these shells.
355
348
:::
349
+
::::
350
+
351
+
:::::
352
+
353
+
356
354
357
355
### Combining dependency groups
358
356
359
-
You can create combined groups that reference other groups:
357
+
You can also create combined groups that reference other groups:
358
+
360
359
```toml
361
360
[project.optional-dependencies]
362
361
test = ["pytest", "pytest-cov"]
363
362
docs = ["sphinx", "pydata-sphinx-theme"]
364
363
dev = ["your-package[test,docs]", "build", "twine"]
365
364
```
366
365
367
-
Then install everything with:
366
+
Then install everything with pip install or uv sync as needed:
368
367
```bash
369
368
uv pip install -e ".[dev]"
370
369
# or
371
370
python -m pip install ".[dev]"
372
371
```
373
372
374
373
:::{tip}
375
-
When you install optional dependencies, pip and uv also install your
374
+
When you install optional dependencies, pip and uv install your
376
375
package and its core dependencies automatically.
377
376
:::
378
377
379
-
:::{tip}
380
-
You can control which versions of dependencies are compatible with your package using specifiers. You will learn more about dependency specifiers in the sections below.
381
-
:::
382
378
383
379
## Version specifiers for dependencies
384
380
@@ -388,11 +384,11 @@ set version ranges.
388
384
389
385
### Common operators
390
386
391
-
-**`>=`**- Minimum version set: `numpy>=1.20` (This is the most common approach and is recommended)
392
-
-**`==`**- Exact version: `requests==2.28.0` (Avoid pinning dependencies like this unless absolutely necessary)
0 commit comments