Skip to content

Commit da6239d

Browse files
committed
Move "reduce backtracking" to dependency resolution topic
1 parent b9f8295 commit da6239d

File tree

2 files changed

+54
-221
lines changed

2 files changed

+54
-221
lines changed

docs/html/topics/dependency-resolution.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,57 @@ Backtracking reduces the risk that installing a new package will accidentally
106106
break an existing installed package, and so reduces the risk that your
107107
environment gets messed up. To do this, pip has to do more work, to find out
108108
which version of a package is a good candidate to install.
109+
110+
## Possible ways to reduce backtracking
111+
112+
There is no one-size-fits-all answer to situations where pip is backtracking
113+
excessively during dependency resolution. There are ways to reduce the
114+
degree to which pip might backtrack though. Nearly all of these approaches
115+
require some amount of trial and error.
116+
117+
### Allow pip to complete its backtracking
118+
119+
In most cases, pip will complete the backtracking process successfully.
120+
This could take a very long time to complete, so this may not be your
121+
preferred option.
122+
123+
However, it is a possible that pip will not be able to find a set of
124+
compatible versions. For this, pip will try every possible combination that
125+
it needs to and determine that there is no compatible set.
126+
127+
If you'd prefer not to wait, you can interrupt pip (Ctrl+c) and try the
128+
strategies listed below.
129+
130+
### Reduce the number of versions pip is trying to use
131+
132+
It is usually a good idea to add constraints the package(s) that pip is backtracking on (e.g. in the above example - `cup`).
133+
134+
You could try:
135+
136+
```
137+
pip install tea "cup >= 3.13"
138+
```
139+
140+
This will reduce the number of versions of `cup` it tries, and
141+
possibly reduce the time pip takes to install.
142+
143+
There is a possibility that the addition constraint is incorrect. When this
144+
happens, the reduced search space makes it easier for pip to more quickly
145+
determine what caused the conflict and present that to the user. It could also
146+
result in pip backtracking on a different package due to some other conflict.
147+
148+
### Use constraint files or lockfiles
149+
150+
This option is a progression of the previous section. It requires users to know
151+
how to inspect:
152+
153+
- the packages they're trying to install
154+
- the package release frequency and compatibility policies
155+
- their release notes and changelogs from past versions
156+
157+
During deployment, you can create a lockfile stating the exact package and
158+
version number for for each dependency of that package. You can create this
159+
with `pip-tools <https://github.com/jazzband/pip-tools/>`\_\_.
160+
161+
This means the "work" is done once during development process, and thus
162+
will avoid performing dependency resolution during deployment.

docs/html/user_guide.rst

Lines changed: 0 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -999,227 +999,6 @@ issue tracker`_ if you believe that your problem has exposed a bug in pip.
999999
.. _"How do I ask a good question?": https://stackoverflow.com/help/how-to-ask
10001000
.. _pip issue tracker: https://github.com/pypa/pip/issues
10011001

1002-
.. _`Dependency resolution backtracking`:
1003-
1004-
Dependency resolution backtracking
1005-
==================================
1006-
1007-
Or more commonly known as *"Why does pip download multiple versions of
1008-
the same package over and over again during an install?"*.
1009-
1010-
The purpose of this section is to provide explanation of why
1011-
backtracking happens, and practical suggestions to pip users who
1012-
encounter it during a ``pip install``.
1013-
1014-
What is backtracking?
1015-
---------------------
1016-
1017-
Backtracking is not a bug, or an unexpected behaviour. It is part of the
1018-
way pip's dependency resolution process works.
1019-
1020-
During a pip install (e.g. ``pip install tea``), pip needs to work out
1021-
the package's dependencies (e.g. ``spoon``, ``hot-water``, ``cup`` etc.), the
1022-
versions of each of these packages it needs to install. For each package
1023-
pip needs to decide which version is a good candidate to install.
1024-
1025-
A "good candidate" means a version of each package that is compatible with all
1026-
the other package versions being installed at the same time.
1027-
1028-
In the case where a package has a lot of versions, arriving at a good
1029-
candidate can take a lot of time. (The amount of time depends on the
1030-
package size, the number of versions pip must try, and other concerns.)
1031-
1032-
How does backtracking work?
1033-
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1034-
1035-
When doing a pip install, pip starts by making assumptions about the
1036-
packages it needs to install. During the install process it needs to check these
1037-
assumptions as it goes along.
1038-
1039-
When pip finds that an assumption is incorrect, it has to try another approach
1040-
(backtrack), which means discarding some of the work that has already been done,
1041-
and going back to choose another path.
1042-
1043-
For example; The user requests ``pip install tea``. ``tea`` has dependencies of
1044-
``cup``, ``hot-water``, ``spoon`` amongst others.
1045-
1046-
pip starts by installing a version of ``cup``. If it finds out it isn’t
1047-
compatible (with the other package versions) it needs to “go back”
1048-
(backtrack) and download an older version.
1049-
1050-
It then tries to install that version. If it is successful, it will continue
1051-
onto the next package. If not it will continue to backtrack until it finds a
1052-
compatible version.
1053-
1054-
This backtrack behaviour can end in 2 ways - either 1) it will
1055-
successfully find a set of packages it can install (good news!), or 2) it will
1056-
eventually display a `resolution impossible <https://pip.pypa.io/en/latest/user_guide/#id35>`__ error
1057-
message (not so good).
1058-
1059-
If pip starts backtracking during dependency resolution, it does not
1060-
know how long it will backtrack, and how much computation would be
1061-
needed. For the user this means it can take a long time to complete.
1062-
1063-
Why does backtracking occur?
1064-
----------------------------
1065-
1066-
With the release of the new resolver (:ref:`Resolver changes 2020`), pip is now
1067-
more strict in the package versions it installs when a user runs a
1068-
``pip install`` command.
1069-
1070-
Pip needs to backtrack because initially, it doesn't have all the information it
1071-
needs to work out the correct set of packages. This is because package indexes
1072-
don't provide full package dependency information before you have downloaded
1073-
the package.
1074-
1075-
This new resolver behaviour means that pip works harder to find out which
1076-
version of a package is a good candidate to install. It reduces the risk that
1077-
installing a new package will accidentally break an existing installed package,
1078-
and so reduces the risk that your environment gets messed up.
1079-
1080-
What does this behaviour look like?
1081-
-----------------------------------
1082-
1083-
Right now backtracking behaviour looks like this:
1084-
1085-
::
1086-
1087-
$ pip install tea==1.9.8
1088-
Collecting tea==1.9.8
1089-
Downloading tea-1.9.8-py2.py3-none-any.whl (346 kB)
1090-
|████████████████████████████████| 346 kB 10.4 MB/s
1091-
Collecting spoon==2.27.0
1092-
Downloading spoon-2.27.0-py2.py3-none-any.whl (312 kB)
1093-
|████████████████████████████████| 312 kB 19.2 MB/s
1094-
Collecting hot-water>=0.1.9
1095-
Downloading hot-water-0.1.13-py3-none-any.whl (9.3 kB)
1096-
Collecting cup>=1.6.0
1097-
Downloading cup-3.22.0-py2.py3-none-any.whl (397 kB)
1098-
|████████████████████████████████| 397 kB 28.2 MB/s
1099-
INFO: pip is looking at multiple versions of this package to determine
1100-
which version is compatible with other requirements.
1101-
This could take a while.
1102-
Downloading cup-3.21.0-py2.py3-none-any.whl (395 kB)
1103-
|████████████████████████████████| 395 kB 27.0 MB/s
1104-
Downloading cup-3.20.0-py2.py3-none-any.whl (394 kB)
1105-
|████████████████████████████████| 394 kB 24.4 MB/s
1106-
Downloading cup-3.19.1-py2.py3-none-any.whl (394 kB)
1107-
|████████████████████████████████| 394 kB 21.3 MB/s
1108-
Downloading cup-3.19.0-py2.py3-none-any.whl (394 kB)
1109-
|████████████████████████████████| 394 kB 26.2 MB/s
1110-
Downloading cup-3.18.0-py2.py3-none-any.whl (393 kB)
1111-
|████████████████████████████████| 393 kB 22.1 MB/s
1112-
Downloading cup-3.17.0-py2.py3-none-any.whl (382 kB)
1113-
|████████████████████████████████| 382 kB 23.8 MB/s
1114-
Downloading cup-3.16.0-py2.py3-none-any.whl (376 kB)
1115-
|████████████████████████████████| 376 kB 27.5 MB/s
1116-
Downloading cup-3.15.1-py2.py3-none-any.whl (385 kB)
1117-
|████████████████████████████████| 385 kB 30.4 MB/s
1118-
INFO: pip is looking at multiple versions of this package to determine
1119-
which version is compatible with other requirements.
1120-
This could take a while.
1121-
Downloading cup-3.15.0-py2.py3-none-any.whl (378 kB)
1122-
|████████████████████████████████| 378 kB 21.4 MB/s
1123-
Downloading cup-3.14.0-py2.py3-none-any.whl (372 kB)
1124-
|████████████████████████████████| 372 kB 21.1 MB/s
1125-
Downloading cup-3.13.1-py2.py3-none-any.whl (381 kB)
1126-
|████████████████████████████████| 381 kB 21.8 MB/s
1127-
This is taking longer than usual. You might need to provide the
1128-
dependency resolver with stricter constraints to reduce runtime.
1129-
If you want to abort this run, you can press Ctrl + C to do so.
1130-
Downloading cup-3.13.0-py2.py3-none-any.whl (374 kB)
1131-
1132-
In the above sample output, pip had to download multiple versions of
1133-
package ``cup`` - cup-3.22.0 to cup-3.13.0 - to find a version that will be
1134-
compatible with the other packages - ``spoon``, ``hot-water``, etc.
1135-
1136-
These multiple ``Downloading cup-version`` lines show pip backtracking.
1137-
1138-
Possible ways to reduce backtracking occurring
1139-
----------------------------------------------
1140-
1141-
It's important to mention backtracking behaviour is expected during a
1142-
``pip install`` process. What pip is trying to do is complicated - it is
1143-
working through potentially millions of package versions to identify the
1144-
compatible versions.
1145-
1146-
There is no guaranteed solution to backtracking but you can reduce it -
1147-
here are a number of ways.
1148-
1149-
.. _1-allow-pip-to-complete-its-backtracking:
1150-
1151-
1. Allow pip to complete its backtracking
1152-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1153-
1154-
In most cases, pip will complete the backtracking process successfully.
1155-
It is possible this could take a very long time to complete - this may
1156-
not be your preferred option.
1157-
1158-
However, there is a possibility pip will not be able to find a set of
1159-
compatible versions.
1160-
1161-
If you'd prefer not to wait, you can interrupt pip (ctrl and c) and use
1162-
:ref:`Constraints Files`: to reduce the number of package versions it tries.
1163-
1164-
.. _2-reduce-the-versions-of-the-backtracking-package:
1165-
1166-
2. Reduce the number of versions pip will try to backtrack through
1167-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1168-
1169-
If pip is backtracking more than you'd like, the next option is to
1170-
constrain the number of package versions it tries.
1171-
1172-
A first good candidate for this constraining is the package(s) it is
1173-
backtracking on (e.g. in the above example - ``cup``).
1174-
1175-
You could try:
1176-
1177-
``pip install tea "cup > 3.13"``
1178-
1179-
This will reduce the number of versions of ``cup`` it tries, and
1180-
possibly reduce the time pip takes to install.
1181-
1182-
There is a possibility that if you're wrong (in this case an older
1183-
version would have worked) then you missed the chance to use it. This
1184-
can be trial and error.
1185-
1186-
.. _3-use-constraint-files-or-lockfiles:
1187-
1188-
3. Use constraint files or lockfiles
1189-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1190-
1191-
This option is a progression of 2 above. It requires users to know how
1192-
to inspect:
1193-
1194-
- the packages they're trying to install
1195-
- the package release frequency and compatibility policies
1196-
- their release notes and changelogs from past versions
1197-
1198-
During deployment, you can create a lockfile stating the exact package and
1199-
version number for for each dependency of that package. You can create this
1200-
with `pip-tools <https://github.com/jazzband/pip-tools/>`__.
1201-
1202-
This means the "work" is done once during development process, and so
1203-
will save users this work during deployment.
1204-
1205-
The pip team is not available to provide support in helping you create a
1206-
suitable constraints file.
1207-
1208-
.. _4-be-more-strict-on-package-dependencies-during-development:
1209-
1210-
4. Be more strict on package dependencies during development
1211-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212-
1213-
For package maintainers during software development, give pip some help by
1214-
creating constraint files for the dependency tree. This will reduce the
1215-
number of versions it will try.
1216-
1217-
Getting help
1218-
------------
1219-
1220-
If none of the suggestions above work for you, we recommend that you ask
1221-
for help. :ref:`Getting help`.
1222-
12231002
.. _`Using pip from your program`:
12241003

12251004
Using pip from your program

0 commit comments

Comments
 (0)