uv: `uv pip` does not read all system packages (e.g., in `uv pip list --system` or virtual environments with `system-site-packages`)

Executing uv pip list --system does not list any packages. Similarly for freeze and show subcommands (I did not test any others).

$ uv pip list --system -vv
 uv_interpreter::find_python::find_default_python 
      0.000148s   0ms DEBUG uv_interpreter::find_python Starting interpreter discovery for default Python
      0.000319s   0ms DEBUG uv_interpreter::interpreter Cached interpreter info for Python 3.12.2, skipping probing: /usr/bin/python3
    0.000334s DEBUG uv::commands::pip_list Using Python 3.12.2 environment at /usr/bin/python3

OS: Fedora 39, python 3.12, amd64 uv version: 0.1.21, installed via pipx

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 6
  • Comments: 33 (29 by maintainers)

Commits related to this issue

Most upvoted comments

I just ran into a variant of this problem when working with layered environments that use sitecustomize.py to incorporate additional shared folders: when uv is installing into the upper environments, it fails to detect the packages that are already installed in the lower layers. (My use case is along the same lines as the HPC use case above: shared layers with big dependencies preinstalled, then smaller application environments built on top of that)

I initially thought it was working correctly because uv pip install is so much faster than pip install, but the problem became clear when archiving the upper layers slowed down dramatically (unexpectedly including additional copies of torch and nvidia-cuda in an archive is… not great).

Between *.pth files and sitecustomize.py, the only reliable way to find out what packages a Python environment can currently see installed is to ask it for its sys.path value and scan every entry in order, it simply isn’t correct to assume that the only directories that matter are the ones where installation tools are told to put new components.

Running python -m site is a useful way of dumping the relevant sys.path info when investigating problems along these lines (although python -c "import sys; print(sys.path)" is better for programmatic use, as was done in #3500).

Using sitecustomize.py files to reproduce the problem should be much easier to manage in test cases than relying specifically on Linux distro Python builds that add extra directories to sys.path.

I don’t advise trying to use *.pth files (even in test cases), since they have problems dealing with non-ASCII paths in Python versions prior to 3.13.

Edit: for anyone curious, the specific environment layering project referred to here is https://pypi.org/project/venvstacks/

@danielhollas can you take a look to see if i’m delirious or this has indeed been fixed 🦀

@ChannyClaus to clarify, this has always worked. 😃 In other words, this issue is about uv ignoring Fedora system packages that come with the Fedora installation, which are in a different directory from system packages installed by the user. (I am assuming they are doing this to prevent users from messing up the system python install).

the change i’ve been working on which uses sys.path (thanks again charlie for that pointer!) does pick up a bit more but may be somewhat unnecessary since all packages installed by pip seem to already be picked up without it (below is the snippet with the sys.path change, where uv picks up dnf and libdnf, which are not picked up with the latest version of uv).

I’d still argue that uv should list all the packages that are available to the Python interpreter.

I noticed the following help text for the uv venv --system-site-packages

 --system-site-package
   Give the virtual environment access to the system site packages directory.
          
   Unlike `pip`, when a virtual environment is created with `--system-site-packages`, 
   `uv` will _not_ take system site packages into account when running commands 
   like `uv pip list` or `uv pip install`. 
   The `--system-site-packages` flag will provide the virtual environment
   with access to the system site packages directory at runtime, 
   but it will not affect the behavior of `uv` commands.

This is very much the same issue as here, but from the text it is not clear whether this was a conscious design decision or just stating the current limitation. If the former I’d argue to reconsider. As an example, in HPC context, admins may provide a base Python environment with basic scientific packages (numpy et al) compiled specifically for the machine to get the best performance. In this case, it would be quite wrong for uv to ignore these packages and reinstall them inside the venv.

For the test suite, I’d recommend testing the sitecustomize.py case rather than the system-site-packages case:

  1. Create a pair of regular (isolated) virtual environments (called “A” and “B” below)
  2. In environment B, inject a sitecustomize.py file into site-packages that uses site.addsitedir to integrate the site-packages folder from environment A into environment B
  3. Write the test cases to manipulate environment B ensuring that:
  • list/show/freeze can see the packages in both environments
  • if an installation request or dependency is already satisfied in environment A, it doesn’t get installed into B
  • if a package is upgraded in environment B, it is installed into B without attempting to uninstall it from A
  • attempting to remove a package that is only present in A via environment B fails (<-- I haven’t actually checked how pip uninstall handles this case, but refusing to uninstall seems preferable no matter what pip does)

If that works as described, a venv with system-site-packages enabled should also work (without the hassle of trying to make tests that enable system-site-packages portable across different Python installations).

i can probably look into this sometime this week (maybe weekend) unless someone else is already on it :.)

(at least based on the last time i gave this a try)

@ChannyClaus do you happen to have a draft PR/commit ref handy for this? If you have the logic side of the change ready to go, I’d be more than happy to give the test suite a whirl 😃

things may have changed significantly since this was months ago but the main change necessary was https://github.com/astral-sh/uv/pull/2636/files#diff-f29dcb297374a7babe6be50ec410d36c52cb44c6e9ec5183b4b7d0b96229bd94R94-R109 i believe 🌵

(To summarize, I think the fix here is to iterate over sys.paths (probably) instead of just looking at purelib and platlib.)

Hmm, strange. I am running directly on Fedora 39, I am wondering if there are some differences between Fedora 39 Docker image and normal install. Also in your case you’re running under root, not sure if that makes a difference. I’ll try to dig around a bit more.

Indeed, installing something into system packages will install it to /usr/local/lib

$ sudo uv pip install tuna --system
Resolved 1 package in 261ms
Downloaded 1 package in 99ms
Installed 1 package in 3ms
 + tuna==0.5.11

$ uv pip list
Package Version
------- -------
tuna    0.5.11

$ uv pip show tuna
Name: tuna
Version: 0.5.11
Location: /usr/local/lib/python3.12/site-packages
Requires:

Here’s how the site directories are configured in my Python (Fedora 39)

$ python -c "import site;print(site.getsitepackages())"
['/usr/local/lib64/python3.12/site-packages', '/usr/local/lib/python3.12/site-packages', '/usr/lib64/python3.12/site-packages', '/usr/lib/python3.12/site-packages']

(To summarize, I think the fix here is to iterate over sys.paths (probably) instead of just looking at purelib and platlib.)

Yep, that seems to be the case.