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

docs: improve Python docs, add section about PEP 668 #16643

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions docs/Homebrew-and-Python.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

This page describes how Python is handled in Homebrew for users. See [Python for Formula Authors](Python-for-Formula-Authors.md) for advice on writing formulae to install packages written in Python.

Homebrew should work with any [CPython](https://stackoverflow.com/questions/2324208/is-there-any-difference-between-cpython-and-python) and defaults to the macOS system Python.
Homebrew will install the necessary Python 3 version that is needed to make your packages work. Python 2 (or 1) is not supported.

Homebrew provides formulae to brew Python 3.y. A `python@2` formula was provided until the end of 2019, at which point it was removed due to the Python 2 deprecation.
iMichka marked this conversation as resolved.
Show resolved Hide resolved
## Python 3

**Important:** If you choose to use a Python which isn't either of these two (system Python or brewed Python), the Homebrew team cannot support any breakage that may occur.

## Python 3.y

Homebrew provides formulae for maintained releases of Python 3.y (`[email protected]`).
Homebrew provides formulae for the newest and maintained releases of Python 3 (`[email protected]`) (https://devguide.python.org/versions/).
We keep older `[email protected]` versions according to our [versioned formulae guidelines](https://docs.brew.sh/Versions).

**Important:** Python may be upgraded to a newer version at any time. Consider using a version
manager such as `pyenv` if you require stability of minor or patch versions for virtual environments.
Expand All @@ -26,11 +23,13 @@ Unversioned symlinks for `python`, `python-config`, `pip` etc. are installed her
$(brew --prefix python)/libexec/bin
```

**Warning!** The executables do not always point to the latest Python 3 version, as there is always a delay between the newest Python 3 release and the homebrew-core repository switching to the newest version.

## Setuptools, pip, etc.

The Python formulae install [pip](https://pip.pypa.io/) (as `pip3`) and [Setuptools](https://pypi.org/project/setuptools/).

Setuptools can be updated via `pip`, without having to re-brew Python:
Setuptools can be updated via `pip`, without having to reinstall brewed Python:

```sh
python3 -m pip install --upgrade setuptools
Expand Down Expand Up @@ -73,6 +72,8 @@ Some formulae provide Python bindings.

These should be installed via `pip install <package>`. To discover, you can use <https://pypi.org/search>.

Starting with Python 3.12, we highly recommend you to use a separate virtualenv for this (see the section about [PEP 668](https://peps.python.org/pep-0668/#marking-an-interpreter-as-using-an-external-package-manager) below).

**Note:** macOS's system Python does not provide `pip`. Follow the [pip documentation](https://pip.pypa.io/en/stable/installation/) to install it for your system Python if you would like it.

## Brewed Python modules
Expand All @@ -85,13 +86,22 @@ Since the system Python may not know which compiler flags to set when building b
CFLAGS="-I$(brew --prefix)/include" LDFLAGS="-L$(brew --prefix)/lib" pip install <package>
```

## Virtualenv

**Warning!** When you `brew install` formulae that provide Python bindings, you should **not be in an active virtual environment.**

Activate the virtualenv *after* you've brewed, or brew in a fresh terminal window. This will ensure Python modules are installed into Homebrew's `site-packages` and *not* into that of the virtual environment.
Activate the virtualenv *after* you have installed your package with brew, or install brew's packages in a fresh terminal window. This will ensure Python modules are installed into Homebrew's `site-packages` and *not* into that of the virtual environment.

## PEP 668 ([email protected]) and virtualenvs

Starting with [email protected], Homebrew follows [PEP 668](https://peps.python.org/pep-0668/#marking-an-interpreter-as-using-an-external-package-manager).

If you wish to install a non-brew-packaged Python package (from PyPI for example):

* create a virtual environment using `python3 -m venv path/to/venv`. Then use `path/to/venv/bin/python` and `path/to/venv/bin/pip`.
* or use `pipx install xyz`, which will manage a virtual environment for you.
You can install `pipx` by running `brew install pipx`.
When you use `pipx` to install a Python application, it will always use a virtual environment for you.

Virtualenv has a `--system-site-packages` switch to allow "global" (i.e. Homebrew's) `site-packages` to be accessible from within the virtualenv.
It is possible to install some Python packages as formulae, by using `brew install xyz`. We do not recommend using these formulae and recommend you install them with pip using a virtualenv. These syste-wide Hombrew Python formulae are often Homebrew-specific formulae that are useful as dependencies for other Homebrew formulae. It is not recommended to rely on them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some typos to fix for future PR:

Suggested change
It is possible to install some Python packages as formulae, by using `brew install xyz`. We do not recommend using these formulae and recommend you install them with pip using a virtualenv. These syste-wide Hombrew Python formulae are often Homebrew-specific formulae that are useful as dependencies for other Homebrew formulae. It is not recommended to rely on them.
It is possible to install some Python packages as formulae, by using `brew install xyz`. We do not recommend using these formulae and recommend you install them with pip using a virtualenv. These system-wide Homebrew Python formulae are often Homebrew-specific formulae that are useful as dependencies for other Homebrew formulae. It is not recommended to rely on them.


## Why is Homebrew's Python being installed as a dependency?

Expand Down
Loading