Clean install of Python 3.12 on Mac (macOS Sonoma)

Been a while since I last played with Python. But want to start automating some of my daily web data tasks and add a bit of local AI to it.

https://xkcd.com/1987

Setting up a clean environment for these. Been too lazy over the years with Macs, kind of forgot how messy a system can get under the hood after a while.

So I am starting with Python reinstall. You can check for the latest version here. https://www.python.org/downloads/

Useful commands to check a few things before you start:

python --version
pip --version
which -a python

---
You might need to be specific to know
python3 --version
pip3 --version
which -a python3

---
For older macOS, you might want to check previous default installation too
ls -l /usr/local/bin/ | grep python*
ls -l /usr/bin/python*

I had quite a few different versions installed on my system. So I went ahead to delete them all. Then reinstalled using brew.

sudo rm /usr/local/bin/python*
sudo rm /usr/local/bin/pip*
sudo rm /usr/local/bin/easy*

Python 3.9.6 is the version of Python bundled with macOS Sonoma 14. Try not to remove this from your system (don’t think you can anyway, but shouldn’t try).

https://code2care.org/macos/default-python-version-macos-sonoma-14


Installing Pyenv to manage python environment and use it to set your global version. https://stackoverflow.com/a/60125308

brew install pyenv
pyenv install 3.12.5
pyenv global 3.12.5

Or follow the instructions on this page https://github.com/pyenv/pyenv-installer to compile from source.

If you upgraded your Mac from Intel based to Apple Silicon using Migration Assistant, you might still have these files. Delete them before you try to compile from source.

/usr/local/lib/libgettextlib.dylib
/usr/local/lib/libintl.dylib
/usr/local/include/libintl.h

https://github.com/pyenv/pyenv/issues/2343

Set the right PATH

You are on Sonoma, so you will be using zshrc and zprofile (older version bachrc vs. bash_profile). You will need to set the right path there. Very quick answer is: .zprofile is sourced upon login.
.zshrc is sourced upon starting of a new shell.

I set mine to just one line: (reminder, there is no space in the path)

export PATH='/usr/local/bin:/usr/local/sbin:/usr/bin:$PATH'

I have heard about Rye, but haven’t got around to trying it yet.
Still using pyenv for now.
https://www.freecodecamp.org/news/how-to-install-python-on-a-mac/
https://mac.install.guide/python/install-pyenv
https://github.com/pyenv/pyenv


Reference: