# Trying to understand the details of \`pip wheel\`

**URL:** <https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016>\
**Category:** Packaging\
**Created:** [November 18, 2023, 12:27am UTC](https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016 "2023-11-18T00:27:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jens](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jens/32/5052_2.png) [@jens](https://discuss.python.org/u/jens)\
**Post date:** [November 18, 2023, 12:27am UTC](https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016/1 "2023-11-18T00:27:24Z")

</div>

Hello everyone,

I’d like to follow up on a recent issue I encountered in a project of mine (issue [#644](https://github.com/jenstroeger/python-package-template/issues/644)) and understand a few more details on how `pip wheel` works exactly.

For context: given a venv where I’ve installed a package in editable mode and all of its dependencies (for local development) chances are I’ve also installed other packages to toy and tinker and experiment. However, now I’d like to _prune back_ all installed packages and remove those which aren’t direct or indirect deps of my editable package. Or phrased differently: I’d like to remove those packages that can’t be reached from my package’s dependency graph.

To do that, I came up with the following (actual source [here](https://github.com/jenstroeger/python-package-template/blob/d4544af5a74bfb0763321c1094ebcc68b2d91ff7/Makefile#L210-L223)):

1. Create a `requirements.txt` file of all currently installed packages.
2. Build wheels for all of them into a local wheelhouse folder.
3. Uninstall all packages using the `requirements.txt` file.
4. Install the package from the local wheelhouse folder and _without_ a package index.

Most of the time that works quite well. Except — and hence the issue and this question 🤓 — on some (rare) occasion step 2 seems to pull in more package versions of an already installed package to run the deps resolver. And _that_ can go awry and take a long time.

Which raises two questions: Why would `pip wheel` need more dependency information (and download packages) than it already has available in my venv? And is there a better way to prune away non-dependent packages in a venv?

Thank you in advance for your thoughts,  
Jens

---

<div class="post-metadata">

**Author:** ![jwodder](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jwodder/32/236_2.png) [@jwodder](https://discuss.python.org/u/jwodder)\
**Post date:** [November 18, 2023, 12:47am UTC](https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016/2 "2023-11-18T00:47:00Z")

</div>

Regarding the point about pruning venvs, I can think of two better approaches:

- Just delete the venv and recreate it with only the package(s) you want. Virtualenvs are meant to be disposable, after all.

- Parse the output from [pipdeptree](https://github.com/tox-dev/pipdeptree)’s JSON mode to determine which packages aren’t needed by the packages you want to keep, and then unininstall those. You don’t even need to install pipdeptree in the venv you’re analysing.

As for your primary question, I think it’d be hard to answer without at least the pip logs from an actual occurrence of the problem. One thing I can think of is that some package that doesn’t provide wheels for your platform has an sdist that builds nondeterministically.

Alternatively, depending on what exactly you’re seeing, it may be that pip’s resolver isn’t taking the exact versions in the requirements file fully into account from the start, and pip ends up backtracking through some package’s versions in order to find one that’s compatible with the packages processed so far, which may? (I’m not sure) involve building wheels for intermediate versions.

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [November 18, 2023, 12:55am UTC](https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016/3 "2023-11-18T00:55:51Z")

</div>

I think there is a fundamental misunderstanding going on here. `pip wheel` does not build a wheel from an installed package – there’s just no way you can do that, as a wheel must be built from the source code of the package (for example, your venv doesn’t contain the pyproject.toml of each package). If I understand your post correctly, you’re trying to do it in such a way that PyPI is never involved, which you think you’ve solved because step 4 does indeed not involve it – but step 2 very much does. And it does not use your venv in any way.

---

<div class="post-metadata">

**Author:** ![ketozhang](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@ketozhang](https://discuss.python.org/u/ketozhang)\
**Post date:** [November 18, 2023, 1:19am UTC](https://discuss.python.org/t/trying-to-understand-the-details-of-pip-wheel/39016/4 "2023-11-18T01:19:19Z")

</div>

Adding onto @jeanas.

`pip wheel` only looks at the dependencies you give it (e.g., requirements file).

Pip does not know if the dependencies you pass needs dependencies themselves, so it must run the resolver to check. Adding `--no-deps` will skip this.

If your environment still have the pip cache for all wheels built/downloaded during your venv install, then no downloads are done. I’m not sure if you can disable downloads for offline builds.
