Skip to content

Commit 3656543

Browse files
committed
Fix: more edits from review, new image and add todo extension to sphinx
1 parent cd95be8 commit 3656543

File tree

3 files changed

+84
-42
lines changed

3 files changed

+84
-42
lines changed

conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"sphinx_design",
3535
"sphinx_copybutton",
3636
"sphinx.ext.intersphinx",
37+
"sphinx.ext.todo",
3738
"sphinx_sitemap",
3839
"sphinxext.opengraph",
3940
]
150 KB
Loading

package-structure-code/declare-dependencies.md

Lines changed: 83 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,82 @@
33
: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.
44
```
55

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.
11+
12+
Following this comment:
13+
https://github.com/pyOpenSci/python-package-guide/pull/106#pullrequestreview-1766663571
14+
15+
Jonny will add a section that talks about:
16+
17+
Why you specify dependencies
18+
How to specify dependencies
19+
When you use different specifiers
20+
:::
21+
622
# Python Package Dependencies
723

824
## What is a package dependency?
925

1026
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:
1428

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
1532

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.
1935

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.
2236

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`.
2339

24-
:::{figure-md} python-package-dependencies
40+
You will learn about both below.
41+
42+
:::{figure-md} python-package-dependency-types
2543

26-
<img src="../images/python-package-dependencies.png" alt="">
44+
<img src="../images/python-package-dependency-types.png" alt="">
2745

28-
Caption here
46+
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.
2948
:::
3049

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+
3165
When a Python project is installed, the Python package manager (either `pip`
3266
or `conda`) installs your package's dependencies automatically. This ensures
3367
that when you call a function in a specific dependency, it is available in your
3468
user's environment.
3569

70+
:::{admonition} Dependencies can be added to your pyproject.toml file
71+
3672
In the [pyproject.toml overview page](pyproject-toml-python-package-metadata),
3773
you learned how to set up a **pyproject.toml** file with basic metadata
3874
for your package. On this page, you will learn how to specify different types of
3975
dependencies in your `pyproject.toml`.
4076

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.
44-
45-
Following this comment:
46-
https://github.com/pyOpenSci/python-package-guide/pull/106#pullrequestreview-1766663571
47-
48-
Jonny will add a section that talks about:
49-
50-
Why you specify dependencies
51-
How to specify dependencies
52-
When you use different specifiers
5377
:::
5478

5579
## How do you declare dependencies?
5680

57-
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.
5882
This ensures that all of the metadata associated with your package is declared
5983
in a single place, making it simpler for users and contributors to understand
6084
your package infrastructure.
@@ -73,11 +97,11 @@ If a project contains extensions written in other languages, you may need a `set
7397
:::
7498

7599

76-
### Required dependencies
100+
### Add required dependencies to your pyproject.toml file
77101

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
81105
`dependencies` array located within the `[project]` table of your
82106
**pyproject.toml** file. This looks something like this:
83107

@@ -95,10 +119,14 @@ dependencies = [
95119
```
96120

97121
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.
101127

128+
Remember that fewer dependencies to install reduces the
129+
likelihood of version mismatches in user environments.
102130

103131
:::{admonition} A dependency example
104132

@@ -118,22 +146,23 @@ Declaring a dependency in your `pyproject.toml` file will ensure that it is list
118146

119147
### Optional dependencies
120148

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:
122150

123151
* running your test suite
124152
* building your documentation
125153
* linting and other code cleanup tools
126154

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.
128157

129158
Optional dependencies can be stored in an
130159
`[optional.dependencies]` table in your **pyproject.toml** file.
131160

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.
133162

134163
## Create optional dependency groups
135164

136-
To declare dependencies in your **pyproject.toml** file:
165+
To declare optional dependencies in your **pyproject.toml** file:
137166

138167
1. Add a `[optional.dependencies]` table to your **pyproject.toml** file.
139168
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
158187
order to upload your project to PyPI
159188
:::
160189

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.
162191

163192
```toml
164193
[project.optional-dependencies]
165194
tests = [
166195
"pytest",
167196
"pytest-cov"
168197
]
169-
docs = ["sphinx", "pydata_sphinx_theme"]
198+
docs = [
199+
"sphinx",
200+
"pydata_sphinx_theme"
201+
]
170202
lint = [
171203
"black",
172204
"flake8"
173205
]
206+
feature = [
207+
"pandas",
208+
]
174209

175210
```
211+
212+
176213
:::{admonition} Additional dependency resources
177214

178215
* [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 = [
183220

184221
### Install dependency groups
185222

186-
:::{admonition} Using `python -m pip`
223+
224+
:::{figure-md} python-package-dependencies
225+
226+
<img src="../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`
187232

188233
In all of the examples in this guide, you will notice we are calling
189234
`pip` using the syntax:
@@ -317,10 +362,6 @@ python:
317362
- docs # you can add any of the subgroups of dependencies from your pyproject.toml file to this list.
318363
```
319364
320-
:::{tip}
321-
322-
:::
323-
324365
325366
:::{admonition} Read the Docs and Python packages
326367
:class: note

0 commit comments

Comments
 (0)