Python 3 on Apple Silicon

Set up Python 3 on Apple Silicon

Kevin Li

--

On Apple Silicon, you can easily download the Intel version of Python and experience low performance due to the overhead of Rosetta translation. Adding fuel to the fire, managing multiple Python versions can be a nightmare. We will demonstrate a simple method to manage multiple Apple-Silicon versions of Python using pyenv to easily switch between versions.

Install pyenv with brew

brew update
brew install pyenv

Set up shell environment for zsh

Execute the following lines in zsh. It will initialize pyenv at the start of every zsh session and set the Python version to be the global Python version specified in pyenv. We will see later about how to set this global version.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

For the changes to take effect in your current session, run source ~/.zshrc`. For other shells or advanced configurations, see the official guide.

Install a version of Python 3

Install the Python 3 version you want through pyenv. As the time of writing, the latest stable Python 3 release is 3.10.6 so we will install that:

pyenv install 3.10.6

After the installation completes, we can check all Python versions installed on pyenv:

pyenv versions

The output should be something like the following:

system
3.10.6 (set by /Users/kevin/.pyenv/version)

Configure global Python version

You can set the global Python version to the one we just installed and override the system version:

pyenv global 3.10.6

Now run pyenv versions again, you will see that the global version is prefixed with an asterisk *:

system
* 3.10.6 (set by /Users/kevin/.pyenv/version)

Now every time you open a new terminal, you will can access the version of Python we just configured through thepython and pip commands. To verify that you have set the global version successfully. Run python -Vand check that it outputs the global version we configured:

Python 3.10.6

Run pip -V and check that the path to pip contains the global version we configured:

pip 22.2.2 from /Users/kevin/.pyenv/versions/3.10.6/lib/python3.10/site-packages/pip (python 3.10)

If those two checks pass, you are good to go on your next Python adventure on Apple Silicon!

--

--

Kevin Li

❤️ Open Source, Web Dev, programming languages, and Hanzi 漢字