-
Notifications
You must be signed in to change notification settings - Fork 805
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 venv
's runtime import patching hooks are unexpected
#6426
Comments
@konstin -- Do you know if we can remove these? |
I frankly don't know, i initially copied this file from https://github.com/gaborbernat/virtualenv/blob/main/src/virtualenv/create/via_global_ref/_virtualenv.py to create identical venvs to pypa/virtualenv (an initial goal in gourgeist), but i can't tell if they have any relevance today still. |
I found some upstream history for the hooks in:
Of note I found this comment: ...which said:
...which suggests at least one of the bugs being worked around by these hooks only occurred with |
Hmm so the And that fix is still there in Python 3.11: Plus in setuptools' adopted copy of distutils: The fix involves identify a venv using My first thought was that perhaps the environments created by And pypa/virtualenv#1663 reports using virtualenv 20. That said, there was a slight deviation from the PEP-405 spec until it was fixed in virtualenv v20.16.7 (Nov 2022): |
from __future__ import annotations I appreciate the Python team no longer support Python 3.6, but RedHat-style Linux distributions will support Python 3.6 until releasever 8 expires in 2029. I'm happy for the community to move on, in general, but find it frustrating when key tools break due to a lack of care or a surplus of spite rather than necessity. Update: it occurs to me Python 3.6 projects might be benefiting from some of the other lines in the VIRTUALENV_PATCH. I'd like to keep those lines until either 2029 or the discovery or provision of some means to patch the environment ourselves. I regret we can't just simply create the file between the environment's creation and use, as they're both happening during |
`_virtualenv.py` doesn't need to import `__future__.annotations`, as it has none. Removing the import: * Restores the action of the VIRTUALENV_PATCH on Python 3.6 * Eliminates 24 lines of error messages displayed by Python 3.6 when it starts in an environment created by uv: ```plaintext Error processing line 1 of /tmp/tmp.ENwqZ0oeyb/lib/python3.6/site-packages/_virtualenv.pth: Traceback (most recent call last): File "~/.pyenv/versions/3.6.15/lib/python3.6/site.py", line 168, in addpackage exec(line) File "<string>", line 1, in <module> File "/tmp/tmp.ENwqZ0oeyb/lib/python3.6/site-packages/_virtualenv.py", line 3 from __future__ import annotations ^ SyntaxError: future feature annotations is not defined Remainder of file ignored ``` (Python displays the errors above twice.) I appreciate the Python team no longer support Python 3.6, but RedHat-style Linux distributions will support Python 3.6 in their `/usr/libexec/platform-python` until [releasever 8 expires in 2029](https://access.redhat.com/support/policy/updates/errata#RHEL8_Planning_Guide). I'm happy for the community to move on, in general, but don't see the harm in helping those who can't. I'm not yet sure what in the “remainder of file ignored” is necessary for my project's build, as I haven't yet finished digging that from under Hatch. I'll follow up on #6426 when I do, so we can concentrate on getting to the happy cow. ## Test Plan ```sh ( set -eu export VIRTUAL_ENV="$(mktemp -d)" ./target/release/uv venv "$VIRTUAL_ENV" --python=python3.6 ./target/release/uv pip install cowsay $VIRTUAL_ENV/bin/python -m cowsay --text 'Look, a talking cow!' ) ``` Happy output: ```plaintext Using Python 3.6.15 interpreter at: ~/.local/bin/python3.6 Creating virtualenv at: /tmp/tmp.VHl4XNi3oI Activate with: source /tmp//tmp.VHl4XNi3oI/bin/activate Resolved 1 package in 929ms Installed 1 package in 17ms + cowsay==6.0 ____________________ | Look, a talking cow! | ==================== \ \ ^__^ (oo)\_______ (__)\ )\/\ ||----w | || || ``` --------- Co-authored-by: Zanie Blue <contact@zanie.dev>
Hi
When a virtual environment is created using
uv venv
, it adds run-time import hooks in the createdsite-packages
, which patchdistutils.dist
and/orsetuptools.dist
:See:
https://github.com/astral-sh/uv/blob/0.3.1/crates/uv-virtualenv/src/virtualenv.rs#L401-L403
https://github.com/astral-sh/uv/blob/0.3.1/crates/uv-virtualenv/src/_virtualenv.py
It turns out these hooks are copied from the
virtualenv
project:https://github.com/pypa/virtualenv/blob/20.26.3/src/virtualenv/create/via_global_ref/_virtualenv.py
This was surprising to me, since:
uv venv
to behave more likepython -m venv
(which doesn't add these hooks) than thevirtualenv
project, given (a) thevenv
subcommand name, (b) thevirtualenv
project to me is more commonly associated with older Python versions (prior to thevenv
module existing) or niche use-cases for seeding or API usage.ensurepip
orvenv
, and so it feels odd to be installing pre-emptive hooks for it.Some thoughts:
venv
feature would include these hooks too?uv venv
provide an option to opt-out of the hooks?uv venv
skip installing these hooks on Python 3.12+, given that setuptools is more likely to not be installed there (due to the upstreamensurepip
andvenv
changes)?uv venv
be looking to align itself with the behaviour of the stdlib's venv module, or the virtualenv project? Which would be least surprising for end-users? (Personally, I would expect to be able to substitute apython -m venv .venv && uv ...
withuv venv && uv ...
and for there to be no change in functionality. I haven't used the separatevirtualenv
project to directly create an environment for years.)The text was updated successfully, but these errors were encountered: