iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐍

Installing Python 3.9.0 using pyenv on macOS Catalina with zsh

に公開
2022/04/24

Setting up the Python Environment

Recently, a younger colleague asked me to "run this Python script for a bit," but since my new MacBook didn't have Python 3.x installed, I went ahead and set up the environment.

I'm just an older guy who doesn't know much about Python, but I do know that "version management is essential!" so I'll be using pyenv (with confidence).

https://zenn.dev/unsoluble_sugar/articles/283ebf698c307c

Checking the Python Version Pre-installed on Mac

By the way, the version of Python pre-installed on macOS Catalina 10.15.6 was 2.7.16.

$ python --version
Python 2.7.16

This is a 2020 MacBook Pro... isn't it about time they included 3.x by default?

Installing pyenv

To support switching between multiple versions, I'll start by installing pyenv. Here, I'll use Homebrew for the installation.

The Missing Package Manager for macOS (or Linux) — Homebrew

If you don't have Homebrew installed yet, run the command mentioned on the official site to install it.

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If Homebrew is already installed, update it first and then install pyenv.

% brew update
% brew install pyenv

It might take a little while, so please be patient.

Adding pyenv to your Path

In macOS Catalina, the default shell is zsh rather than bash, so you need to add the following configuration to your .zshrc file to set the path.

.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Which shell startup file do I put pyenv config in? · pyenv/pyenv Wiki

Checking the pyenv version

Restart your terminal and check the version using the pyenv command.

% pyenv -v
pyenv 1.2.21

If the pyenv command runs successfully, you are good to go.

Checking the list of versions

Let's check the list of Python versions available for installation.

$ pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  ...
  3.9.0
  3.9-dev
  3.10-dev
  activepython-2.7.14
  activepython-3.5.4
  activepython-3.6.0
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1
  ...
  stackless-3.4.7
  stackless-3.5.4
  stackless-3.7.5

A whole bunch of them will show up.

Installing Python 3.9.0

This time, I installed the latest stable version, 3.9.0.

% pyenv install 3.9.0

Checking the version

After the installation is complete, check the list of versions available for switching with pyenv.

$ pyenv versions
* system (set by /Users/unsoluble_sugar/.pyenv/version)
  3.9.0

In addition to the system standard Python version, 3.9.0 has been added.
At this point, Python 2.7.16, which is included by default, remains the current version.

Switching the current version

Use the following command to make 3.9.0 the current version. Here I am using the global specification, but if you want to switch on a per-directory basis, use local instead of global.

% pyenv global 3.9.0

Verifying the version

Check if the version has changed.

% pyenv versions
  system
* 3.9.0 (set by /Users/unsoluble_sugar/.pyenv/version)

Now you can use Python 3.9.0.

It's quite simple.

Discussion