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
+57-18Lines changed: 57 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,52 @@
1
-
# Python package dependency declaration
1
+
# Declaring Development Dependencies - Python Packaging
2
2
3
3
## How to declare documentation, test and other deps
4
4
5
-
It is currently recommended that you store all dependency information in a **pyproject.toml** file.
5
+
In the [pyproject.toml overview page](pyproject-toml-python-package-metadata) we discussed how to setup a pyproject.toml file with basic metadata information.
6
+
On this page, you will learn about storing and accessing dependency information within the pyproject.toml file.
7
+
8
+
It is recommended that you store all dependency information in a **pyproject.toml** file (with a few caveats).
9
+
6
10
This ensures that all of the metadata associated with your package is declared in a single place, making it simpler for users and contributors to understand your package infrastructure.
7
11
8
-
Dependencies for building your documentation, running your tests and building your package's distribution files can be stored in a `[optional.dependencies]` table within the **pyproject.toml** file.
12
+
## Direct project dependencies
13
+
14
+
Your core project dependencies - representing the packages that
15
+
a user requires to install your package, can be stored in a
16
+
dependencies array located within the `[project]` table of your
17
+
pyproject.toml file. This looks something like this:
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 dependnecies that a user needs to run core development elements of your package such as:
35
+
36
+
* running your test suite
37
+
* building your documentation
38
+
* linting and other code cleanup tools
39
+
40
+
These dependencies can be stored in an
41
+
`[optional.dependencies]` table within the **pyproject.toml** file.
9
42
10
43
```{admonition} What happened to the requirements.txt file for dependencies?
11
44
:class: note
12
45
13
-
The requirements.txt file used to be the default way to store dependencies. However in recent years, the ecosystem has moved to storing all of this information in a single **pyproject.toml** file.
14
-
```
46
+
The requirements.txt file used to be the default way to store dependencies. However in recent years, the ecosystem has moved to storing all of this information in a single **pyproject.toml** file. You may find that some projects do still maintain a requirements.txt file either for specific local development needs OR to support users who may want to create a pip-based virtual environment.
47
+
````
48
+
49
+
## How to declare development dependencies
15
50
16
51
To declare dependencies in your **pyproject.toml** file:
17
52
@@ -48,17 +83,22 @@ lint = [
48
83
49
84
## How to install dependencies from your pyproject.toml
50
85
51
-
You can then install the dependencies via individual groups using:
86
+
You can install development dependencies using the
87
+
groups that you defined above using the syntax:
52
88
53
-
`pip install yourPackage.[docs]`
89
+
`pip install .[docs]`
54
90
55
-
or you could install just the dependencies for your tests using:
91
+
Above you install the dependencies needed for your documentation and also your package using pip. Below you
92
+
install just the dependencies needed to run your tests:
56
93
57
-
`pip install yourPackage.[tests]`
94
+
`pip install .[tests]`
58
95
59
96
You can install all dependencies in the `[optional.dependencies]` table using:
60
97
61
-
`yourPackage.[all]`
98
+
`pip install .[docs, tests, lint]`
99
+
100
+
Each time you call `pip install .[groups-here]`, you are also installing your package locally and also any dependencies
101
+
that your package needs / has declared in your pyproject.toml file.
62
102
63
103
```{admonition} For zsh shell users
64
104
:class: tip
@@ -85,9 +125,10 @@ dev = [
85
125
]
86
126
```
87
127
88
-
You can also install subsets of dependencies like this:
128
+
The above allows you to install both the tests and docs dependency lists
129
+
using the command:
89
130
90
-
`pip install .[tests, docs]`
131
+
`pip install .[dev]`
91
132
92
133
```{tip}
93
134
When you install dependencies using the above syntax:
@@ -101,11 +142,9 @@ pip will also install both your package and its core dependencies.
101
142
102
143
The above workflow assumes that you want to publish your package on PyPI. And then you plan to publish to conda-forge (optionally), [by submitting a recipe using greyskull](https://www.pyopensci.org/python-package-guide/package-structure-code/publish-python-package-pypi-conda.html).
103
144
104
-
If you want to support conda users, you may want to also maintain a conda environment that they can use to install your package. Maintaning a conda environment will also help you test that your package installs as you expect into a conda environment.
105
-
106
-
## Read the Docs & Python Environments
145
+
If you want to support conda users, you may want to also maintain a conda environment that they can use to install your package. Maintaining a conda environment will also help you test that your package installs as you expect into a conda environment.
107
146
108
-
TODO: we will link to this config section from the RTD page as well...
147
+
## Read the Docs & project dependencies
109
148
110
149
If you are using readthedocs to build your documentation, then you may need to install your dependencies using a **readthedocs.yaml** file.
111
150
@@ -129,7 +168,7 @@ python:
129
168
130
169
The above should work if you are using a vanilla packaging approach with a tool such as the [PyPA build tool](https://pypa-build.readthedocs.io/en/stable/) and any back-end that works with `build`.
131
170
132
-
If you are using another front-end build tool such as PDM, Hatch or Poetry to manage dependencies, then your approach to installing dependencies may be different. Each tool has its own, slightly different way of declaring dependencies in the **pyproject.toml** file. You can learn more about [configuing RTD for tools such as Poetry and PDM here.](https://docs.readthedocs.io/en/stable/build-customization.html#install-dependencies-with-poetry)
171
+
If you are using another front-end build tool such as PDM, Hatch or Poetry to manage dependencies, then your approach to installing dependencies may be different. Each tool has its own, slightly different way of declaring dependencies in the **pyproject.toml** file. You can learn more about [configuring RTD for tools such as Poetry and PDM here.](https://docs.readthedocs.io/en/stable/build-customization.html#install-dependencies-with-poetry)
133
172
134
173
```{admonition} A note for conda users
135
174
:class: tip
@@ -138,5 +177,5 @@ If you use a conda environment for developing your tool, keep in mind that when
138
177
139
178
Thus, if you are running a conda environment, installing your package in editable mode risks dependency conflicts. This is particularly important if you have a spatial package that required GDAL or has a GDAL supported dependency.
140
179
141
-
Alternatively, you can install your package using `pip install -e . --no-deps` to only install the package. And install the rest of your depenedncies using a conda environment file.
180
+
Alternatively, you can install your package using `pip install -e . --no-deps` to only install the package. And install the rest of your dependencies using a conda environment file.
0 commit comments