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
+83-42Lines changed: 83 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,58 +3,82 @@
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
+
8
+
:::{todo}
9
+
10
+
keep this comment - https://github.com/pyOpenSci/python-package-guide/pull/106#issuecomment-1844278487 in this file for now - jeremiah did a nice inventory of common shells and whether they need quotes or not. it's really comprehensive. but do we want it in the guide?? it's really useful for more advanced users i think.
A Python package dependency refers to an external package or
11
-
software that your Python project requires to function properly.
12
-
A dependency is not part of your project's codebase. It is a package or software that is called
13
-
within the code of your project.
27
+
software that your Python project:
14
28
29
+
1. needs to function properly.
30
+
2. requires if someone wants to develop / work on improving your package locally or
31
+
3. requires if a user wants to add additional functionality (that is not core) to your package
15
32
16
-
### Optional vs. required dependencies
17
-
There are different types of dependencies to consider when creating
18
-
a Python package:
33
+
A dependency is not part of your project's codebase. It is a package or software that is called
34
+
within the code of your project or during development of your package.
19
35
20
-
1.**Core dependencies:** These dependencies are called directly within your package's code. As such they are required in order to run your package.
21
-
2.**Development dependencies**: These are dependencies that are required to support development of your package. They include tools to run tests such as `pytest`, linters (like `flake8` and `ruff`) and code formatters such as `black` and even automation tools such as `nox` or `tox` that run tasks.
22
36
37
+
### Understanding optional vs. required dependencies
38
+
You can think about dependencies as being either optional or required. If they are required, they will be listed in the `[dependency] =` table of your `pyproject.toml` file. If they are optional, they will be listed in the `[optional.dependencies]` table of your `pyproject.toml`.
There are two broad groups of Python package dependencies: those that are optional and those that are required. Required packages are those that a user needs to use your package. Optional dependencies are packages a user can chose to install to add functionality to your package.
47
+
Within those 2 groups, there are three use cases that you can think about. 1. Core dependencies are **required** for a user to use your package. 2. Development dependencies are optional and only needed if someone wants to work on your package locally. 3. Finally feature dependencies are optional and add additional functionality to your package. Not all packages will have feature dependencies.
29
48
:::
30
49
50
+
51
+
### Required (or core) dependencies
52
+
53
+
Required dependencies are called directly within your package's code. On this page we refer to these dependencies
54
+
as **core dependencies** as they are needed in order to run your package. You should place your core or required dependencies in the `[dependency]=` table of your `pyproject.toml` file.
55
+
56
+
### Optional dependencies
57
+
58
+
Optional dependencies dependencies can be optionally installed by users
59
+
depending upon their needs. There are two broad groups of optional dependencies:
60
+
61
+
1.**Development dependencies**: These are dependencies that are required to support development of your package. They include tools to run tests such as `pytest`, linters (like `flake8` and `ruff`) and code formatters such as `black` and even automation tools such as `nox` or `tox` that run tasks.
62
+
63
+
2.**Feature dependencies:** These are dependencies that a user can chose to install to add functionality to your package.
64
+
31
65
When a Python project is installed, the Python package manager (either `pip`
32
66
or `conda`) installs your package's dependencies automatically. This ensures
33
67
that when you call a function in a specific dependency, it is available in your
34
68
user's environment.
35
69
70
+
:::{admonition} Dependencies can be added to your pyproject.toml file
71
+
36
72
In the [pyproject.toml overview page](pyproject-toml-python-package-metadata),
37
73
you learned how to set up a **pyproject.toml** file with basic metadata
38
74
for your package. On this page, you will learn how to specify different types of
39
75
dependencies in your `pyproject.toml`.
40
76
41
-
:::{todo}
42
-
43
-
keep this comment - https://github.com/pyOpenSci/python-package-guide/pull/106#issuecomment-1844278487 in this file for now - jeremiah did a nice inventory of common shells and whether they need quotes or not. it's really comprehensive. but do we want it in the guide?? it's really useful for more advanced users i think.
We suggest that you declare your dependencies using your `pyproject.toml` file.
81
+
We recommend that you declare your dependencies using your `pyproject.toml` file.
58
82
This ensures that all of the metadata associated with your package is declared
59
83
in a single place, making it simpler for users and contributors to understand
60
84
your package infrastructure.
@@ -73,11 +97,11 @@ If a project contains extensions written in other languages, you may need a `set
73
97
:::
74
98
75
99
76
-
### Required dependencies
100
+
### Add required dependencies to your pyproject.toml file
77
101
78
-
Your core project dependencies represent the packages that
79
-
a package manager (`pip` or `conda`) needs to install in order for your package
80
-
to work properly in a user's environment. Dependencies can be stored in a
102
+
Your core project dependencies need to be installed by a
103
+
package manager such as `pip` or `conda` when a user installs your package. You can add those dependencies to
104
+
the
81
105
`dependencies` array located within the `[project]` table of your
82
106
**pyproject.toml** file. This looks something like this:
83
107
@@ -95,10 +119,14 @@ dependencies = [
95
119
```
96
120
97
121
Ideally, you should only list the packages that are
98
-
necessary to install and use your package in the `[dependencies]` section. This minimizes the number of additional packages that your users must install as well as the number of packages that depend upon your package must also install.
99
-
100
-
Remember that fewer dependencies to install reduces the likelihood of version mismatches in user environments.
122
+
necessary to install and use your package in the
123
+
`[dependencies]` section. This minimizes the number of
124
+
additional packages that your users must install as well
125
+
as the number of packages that depend upon your package
126
+
must also install.
101
127
128
+
Remember that fewer dependencies to install reduces the
129
+
likelihood of version mismatches in user environments.
102
130
103
131
:::{admonition} A dependency example
104
132
@@ -118,22 +146,23 @@ Declaring a dependency in your `pyproject.toml` file will ensure that it is list
118
146
119
147
### Optional dependencies
120
148
121
-
Dependencies for building your documentation, running your tests and building your package's distribution files are often referred to as development dependencies. These are the dependencies that a user needs to run core development elements of your package such as:
149
+
Optional dependencies for building your documentation, running your tests and building your package's distribution files are often referred to as development dependencies. These are the dependencies that a user needs to work on your package locally and perform tasks such as:
122
150
123
151
* running your test suite
124
152
* building your documentation
125
153
* linting and other code cleanup tools
126
154
127
-
These dependencies are considered optional, because they are not required to install and use your package.
155
+
These dependencies are considered optional, because they are not required to install and use your package. Feature
156
+
dependencies are considered optional and should also be placed in the `[optional.dependencies]` table.
128
157
129
158
Optional dependencies can be stored in an
130
159
`[optional.dependencies]` table in your **pyproject.toml** file.
131
160
132
-
It's important to note that within the `optional.dependencies` table, you can store additional, optional dependencies within named sub-groups. This is a different table than the dependencies section discussed above which contains a single array with a single list of required packages.
161
+
It's important to note that within the `[optional.dependencies]` table, you can store additional, optional dependencies within named sub-groups. This is a different table than the dependencies array located within the `[project]` table discussed above which contains a single array with a single list of required packages.
133
162
134
163
## Create optional dependency groups
135
164
136
-
To declare dependencies in your **pyproject.toml** file:
165
+
To declare optional dependencies in your **pyproject.toml** file:
137
166
138
167
1. Add a `[optional.dependencies]` table to your **pyproject.toml** file.
139
168
2. Create named groups of dependencies using the syntax:
@@ -158,21 +187,29 @@ project, you will need to point to a specific commit/tag/hash of that repository
158
187
order to upload your project to PyPI
159
188
:::
160
189
161
-
Below we've created three sets of optional dependencies named: tests, docs and lint:
190
+
Below we've created three sets of optional development dependencies named: tests, docs and lint. We've also added a set of feature dependencies.
162
191
163
192
```toml
164
193
[project.optional-dependencies]
165
194
tests = [
166
195
"pytest",
167
196
"pytest-cov"
168
197
]
169
-
docs = ["sphinx", "pydata_sphinx_theme"]
198
+
docs = [
199
+
"sphinx",
200
+
"pydata_sphinx_theme"
201
+
]
170
202
lint = [
171
203
"black",
172
204
"flake8"
173
205
]
206
+
feature = [
207
+
"pandas",
208
+
]
174
209
175
210
```
211
+
212
+
176
213
:::{admonition} Additional dependency resources
177
214
178
215
*[Learn more: View PyPA's overview of declaring optional dependencies](https://packaging.python.org/en/latest/specifications/declaring-project-metadata/#dependencies-optional-dependencies)
@@ -183,7 +220,15 @@ lint = [
183
220
184
221
### Install dependency groups
185
222
186
-
:::{admonition} Using `python -m pip`
223
+
224
+
:::{figure-md} python-package-dependencies
225
+
226
+
<imgsrc="../images/python-package-dependencies.png"alt="Diagram showing a ven diagram with three sections representing the dependency groups listed above - docs feature and tests. In the center it says yourPackage and lists the core dependencies of that package seaborn and numpy. To the right are two arrows. the first shows the command python - m pip install yourPackage. it them shows how installing your package that way installs only the package and the two core dependencies into a users environment. Below is a second arrow with python -m pip install youPackage[tests]. This leads to an environment with both the package dependencies - yourPackage, seanorn and numpy and also the tests dependencies including pytest and pytest-cov ">
227
+
228
+
When a user installs your package locally using python -m pip install yourPackage only your package and it's core dependencies get installed. When they install your package`[tests]` pip will install both your package and its core dependencies plus any of the dependencies listed within the tests array of your `[optional.dependencies]` table.
229
+
:::
230
+
231
+
:::{admonition} Using `python -m pip install` vs. `pip install`
187
232
188
233
In all of the examples in this guide, you will notice we are calling
189
234
`pip` using the syntax:
@@ -317,10 +362,6 @@ python:
317
362
- docs # you can add any of the subgroups of dependencies from your pyproject.toml file to this list.
0 commit comments