pythonhomebrewzsh

Why are there multiple python versions available via homebrew, and how do they relate to package Installation?


I had a problem, which took me way too long to solve. Basically I coudn't import tkinter and kept getting import _tkinter # If this fails your Python may not be configured for Tk

I finally found the solution which was brew install python-tk@3.12

During the search for it I found this:

~ % brew search python                        

==> Formulae
boost-python3
python-lsp-server
python-trove-classifiers
bpython
python-lxml
python-typing-extensions
cyclonedx-python
python-markdown
python-urllib3
ipython
**python-matplotlib** ✔
python-yq
libvirt-python
python-packaging
python@3.8
python@3.9
python@3.10
python@3.11
**python@3.12** ✔
micropython
python-ply
ptpython
python-psutil
python-argcomplete
python-pytz
python-build
python-requests
python-charset-normalizer
python-setuptools
reorder-python-imports
python-dateutil
python-tabulate
wxpython
python-gdbm@3.11
python-gdbm@3.12
cython
python-idna
jython
python-launcher
python-tk@3.9
python-tk@3.10
python-tk@3.11
**python-tk@3.12** ✔

I want to prevent future errors by further understanding how python packages work. I am confused on why theres these 40 versions of python you can download via brew and why isn't it possible just to have python@3.12 and then through my virutal environment with : pip3 install tkinter

(I also tried pip3 install tk - it worked but the package didn't seem to be the same)

What's so different about these python-matplotlib, python-tk@3.12 - why are they installed differently than packages like numpy and why do we need a separate python version to download them?

(Off topic second question if anyone knows -

During this process ChatGPT also always told me to put these environmental variable in the ~/.zshrc -

export LDFLAGS="-L/opt/homebrew/opt/tcl-tk/lib"
export CPPFLAGS="-I/opt/homebrew/opt/tcl-tk/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/tcl-tk/lib/pkgconfig"

Are they even needed and do they have anything good to do with the tkinter installation?)


Solution

  • In general, it is better to install packages to a virtual environment using pip than to install them "globally" (where in this case, "globally" refers to the HomeBrew-managed Python installation, rather than whatever macOS initially installed).

    python-tkinter is a special case. tkinter itself is not available via PyPi, because it's really part of the standard library. However, some Python installations don't install it (as it has an external dependency on Tcl and Tk, neither of which tend to be bundled with Python itself.) As a result, python-tkinter is sort of a "splinter" intended to be installed together with whichever "real" Python package you install with HomeBrew.

    The other packages (like python-request, for example), are best ignored. They are intended for use by Homebrew-managed Python scripts, rather than infrastructure for your own Python projects. (There are those who would argue that any HomeBrew-installed Python isn't meant for your own projects, but only for the use of HomeBrew itself.)