question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use `/usr/local/bin/python3.<minor_version>` if available on macOS as default python interpreter

See original GitHub issue

How 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:open
  • Created 3 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
gaborbernatcommented, Mar 3, 2021

You want get_interpreter from here https://github.com/pypa/virtualenv/blob/main/src/virtualenv/discovery/builtin.py#L60:

from virtualenv.discovery.builtin import get_interpreter

result = get_interpreter('py', [])
print(result)
result.system
PythonInfo(spec=CPython3.9.2.final.0-64, system=/usr/local/opt/python@3.9/bin/python3.9, exe=/Users/bernat/git/github/virtualenv/.tox/4/dev/bin/python, platform=darwin, version='3.9.2 (default, Feb 24 2021, 13:30:36) \n[Clang 12.0.0 (clang-1200.0.32.29)]', encoding_fs_io=utf-8-utf-8)

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.

1reaction
gaborbernatcommented, Mar 1, 2021

I don’t think you want to fix upcoming bugs 🤐

Read more comments on GitHub >

github_iconTop Results From Across the Web

The right and wrong way to set Python 3 as default on a Mac
You do not want the Mac OS X 'default Python' to be 'python3. ... If we use the package installation, a python3 fill...
Read more >
Use `/usr/local/bin/python3.<minor_version>` if available on macOS ...
Use `/usr/local/bin/python3. ` if available on macOS as default python interpreter.
Read more >
How to set Python3 as a default python version on MacOS?
Open the terminal (bash or zsh) whatever shell you are using. Install python-3 using Homebrew (https://brew.sh). Look where it is installed. ...
Read more >
Updating Python on Mac - Stack Overflow
The default Python on OS X shouldn't be messed with as it's used by the OS itself. If your default is 2.6.1 then...
Read more >
Introducing the Python Launcher for Unix
you will end up with a virtual environment using the newest version of Python that can be found on $PATH . For this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found