uv: Virtual environments break on Homebrew upgrades due to using a `Cellar` link
On Homebrew, virtual environments created by uv venv reference the Python installation under Cellar in their interpreter symlink and pyvenv.cfg, which has the full downstream version in its path. These virtual environments break when Homebrew upgrades the respective Python package to the next maintenance release. In recent versions of venv and virtualenv, this issue was resolved by using the stable link under $(brew --prefix)/opt/python@3.x/ instead. For example, on Python 3.11 macOS aarch64 this would be the interpreter in /opt/homebrew/opt/python@3.11/bin.
This shell session demonstrates the problem:
❯ uv venv
❯ readlink .venv/bin/python
/opt/homebrew/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/bin/python3.11
❯ grep ^home .venv/pyvenv.cfg
home = /opt/homebrew/Cellar/python@3.11/3.11.7_1/Frameworks/Python.framework/Versions/3.11/bin
When Homebrew upgrades its python@3.11 package and cleans up the old installation under Cellar, those references in the virtual environment start to dangle.
For comparison, here’s what I get with venv from Homebrew’s python@3.11 installation:
❯ python3.11 -m venv .venv
❯ readlink .venv/bin/python
python3.11
❯ readlink .venv/bin/python3.11
/opt/homebrew/opt/python@3.11/bin/python3.11
❯ grep ^home .venv/pyvenv.cfg
home = /opt/homebrew/opt/python@3.11/bin
And with virtualenv:
❯ virtualenv --version
virtualenv 20.25.0 from ~/.local/pipx/venvs/virtualenv/lib/python3.12/site-packages/virtualenv/__init__.py
❯ virtualenv .venv
❯ readlink .venv/bin/python
/opt/homebrew/opt/python@3.12/bin/python3.12
❯ grep ^home .venv/pyvenv.cfg
home = /opt/homebrew/opt/python@3.12/bin
Affected platforms: Linux and macOS with Homebrew Python
❯ uv --version
uv 0.1.4
❯ gcc -dumpmachine
arm64-apple-darwin23.2.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 8
- Comments: 31 (27 by maintainers)
Commits related to this issue
- Use base executable to set virtualenv Python path (#8481) ## Summary See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into align... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) ## Summary See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into align... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to astral-sh/uv by charliermarsh 2 years ago
- Use base executable to set virtualenv Python path (#8481) See extensive discussion in https://github.com/astral-sh/uv/pull/8433#issuecomment-2430472849. This PR brings us into alignment with the sta... — committed to willingc/uv by charliermarsh 2 years ago
(Gonna put together a table documenting all this.)
I’m fine to change this but it probably requires 0.5.
I’d actually expect that virtualenv change to break this case, if I’m reading it correctly:
So now
virtualenvwould also use the Cellar path, IIUC?I don’t know if it’s quite relevant but they just fixed a bug by storing the resolved absolute path in the metadata file that is generated for virtual environments: https://github.com/pypa/virtualenv/issues/2682
Also, I know all of you are basically Rust experts, but in case I am reading the source correctly please never ever use canonicalize directly as it is literally (I’m not joking) broken on Windows and will cause all of us bugs and unexpected behavior. Please don’t use it. Instead, it’s common to use the dunce crate or the normpath crate when you don’t want to resolve symlinks. See even Armin talking about it.