Use `/usr/local/bin/python3.<minor_version>` if available on macOS as default python interpreter
See original GitHub issueHow would this feature be useful?
See #633, and others. On macOS with Homebrew, our use of sys.executable
to find the default (non-user-specified) python interpreter gives a more specific file location different than /usr/local/bin/python3
(e.g. /usr/local/opt/python@3.9/bin/python3.9
). When this python executable file is used to create a venv, it links to a VERY specific python executable full path that changes with each sub-sub-minor upgrade by Homebrew of its python3 (e.g. /usr/local/Cellar/python@3.9/3.9.2_1/bin/python3.9
)
The upshot of this is that every single time Homebrew ever upgrades its python (more often than new Python sub-minor versions), all of a macOS user’s pipx venvs break, necessitating a run of pipx reinstall-all
. This can happen multiple times a month, or even multiple times a week (see https://github.com/Homebrew/homebrew-core/commits/578501f1556b739170edf4c991ca12a49ac82616/Formula/python%403.9.rb)
See below on macOS. pipx uses sys.executable
as in the example below to find a python interpreter to use to create a venv:
> /usr/local/bin/python3
Python 3.9.2 (default, Feb 24 2021, 13:26:09)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/usr/local/opt/python@3.9/bin/python3.9'
Then using that to create a venv:
> /usr/local/opt/python@3.9/bin/python3.9 -m venv test_venv
> ls -l test_venv/bin/python3.9
[...] test_venv/bin/python3.9@ -> /usr/local/Cellar/python@3.9/3.9.2_1/bin/python3.9
This last ultra-specific path is what breaks pipx venvs when Homebrew upgrades its python. (Before Homebrew version 3.9.2_1 there were 3.91, 3.91_1, 3.91_2, 3.91_3, 3.91_4, 3.91_5, 3.91_6, 3.91_7, 3.91_8)
Describe the solution you’d like
If we notice that we are on macOS, and we find that /usr/local/bin/python3.<minor_version>
exists, we should use THAT as the default python interpreter on macOS.
In this case we get longer-lived valid symlinks in each venv
> /usr/local/bin/python3.9 -m venv test_venv
> ls -l test_venv/bin/python3.9
[...] test_venv/bin/python3.9@ -> /usr/local/bin/python3.9
Describe alternatives you’ve considered
Otherwise we keep forcing our macOS users (like me) to keep executing pipx reinstall-all
for ever sub-sub-minor Homebrew python upgrade.
Issue Analytics
- State:
- Created 3 years ago
- Comments:17 (17 by maintainers)
Top GitHub Comments
You want get_interpreter from here https://github.com/pypa/virtualenv/blob/main/src/virtualenv/discovery/builtin.py#L60:
Note this might not work exactly you’d want, so we might need to add one or two tweak knobs in there. The app data is there for cache purposes, if you don’t want to cache you can leave it None.
I don’t think you want to fix upcoming bugs 🤐