Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uv run --with fails to include virtual environment in sys.path for Homebrew-installed Python #7152

Closed
dhduvall opened this issue Sep 7, 2024 · 1 comment · Fixed by #7161

Comments

@dhduvall
Copy link

dhduvall commented Sep 7, 2024

I'm trying to run uv run --with pylint -- pylint ..., and pylint is complaining about not being able to resolve import statements in my source. Indeed, sys.path in that context doesn't have the virtual environment's packages.

[project]
name = "test"
version = "1.2.3"
requires-python = "==3.11.*"
dependencies = [
  "regex", # or whatever
]
$ uv run --with pylint -- python -c 'import sys; print("\n".join(sys.path))'
Using Python 3.11.9 interpreter at: /opt/homebrew/opt/python@3.11/bin/python3.11
Removed virtual environment at: .venv
Creating virtualenv at: .venv
Installed 1 package in 4ms

/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python311.zip
/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11
/opt/homebrew/Cellar/python@3.11/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload
/Users/duvall/Library/Caches/uv/archive-v0/rpeJdmPWBfImmHdlLv7hN/lib/python3.11/site-packages
$ uv run --with pylint -- python -c "import regex"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'regex'

Interestingly, this works fine with the Python 3.9 installed on the system, but not on any version installed by Homebrew (3.9 included):

uv run --with pylint python -c 'import sys; print("\n".join(sys.path))'
Using Python 3.9.6 interpreter at: /Library/Developer/CommandLineTools/usr/bin/python3
Removed virtual environment at: .venv
Creating virtualenv at: .venv
Installed 1 package in 2ms

/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python39.zip
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload
/Users/duvall/Library/Caches/uv/archive-v0/AiY6Z--H2Ja4w3r7Vaquv/lib/python3.9/site-packages
/Users/duvall/test/.venv/lib/python3.9/site-packages
$ uv --version
uv 0.4.6 (Homebrew 2024-09-05)

though I was seeing this with 0.4.4 as well.

@bluss
Copy link
Contributor

bluss commented Sep 7, 2024

I think this has something to do with sitecustomize.py - homebrew pythons are for some reason installing such a file? And it interferes with how uv is creating the overlay effect (ephemeral virtualenv layered on top of .venv).

breadcrumbs

Homebrew/homebrew-core#133490

https://docs.brew.sh/Homebrew-and-Python

charliermarsh pushed a commit that referenced this issue Sep 9, 2024
## Summary

Use a path file (`.pth`) instead of `sitecustomize.py` for configuring
path in emphemeral virtualenvs, overlaying the ephemeral venv on top of
the base `.venv`.

`sitecustomize.py` is a module in the python installation and as such a
unique resource - homebrew pythons on macos already install such a file
and thus uv's `sitecustomize.py`, placed in the ephemeral env, did not
have any effect.

I don't find any documentation explicitly saying that addsitedir is
valid in `.pth` files but from trial it seems to be - and there is the
precedent of the existing _virtualenv.pth _virtualenv.py pair that do
nontrivial operations.

## Test Plan

- Testing on ephemeral venv, resolving to base venv including editable
install in base: done (py3.7, 3.12)
- Testing on homebrew python/macos: done (py3.11)
- tests: run_editable

Fixes #7152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants