How to setup Python3 🐍 in a convenient way

Photo by Shahadat Rahman / Unsplash

As I described in the previous post “How to undo messed up Python3 🐍 setup”, I’ve cleaned up whatever was inconveniently placed and now I’m going to through the process of setting up Python3 that will make work with Python more manageable and consistent.

How to undo messed up Python3 setup
I’ve recently got myself a new shiny Apple MacBook Air (M1) and wanted to setup everything I normally use in a proper way from the start. I am a person who uses different programming languages for different purposes and the usual set includes NodeJS, Ruby, Python with/for different

Install (upgrade) system Python - a better way

To make sure I’m never going to be using python3 or pip3 commands with or related inconvenience, I’m going to go and manage Python 🐍 versions with pyenv

More about PYENV 👇

GitHub - pyenv/pyenv: Simple Python version management
Simple Python version management. Contribute to pyenv/pyenv development by creating an account on GitHub.

Install PyEnv

Since I’ve already got Homebrew (and generally use), I will go with:
$ brew install pyenv
🍺 /opt/homebrew/Cellar/pyenv/2.1.0: 819 files, 2.8MB

Install Python 🐍

Latest version?
To find out either go to python.org
or
check with pyenv:

  1. $ brew update && brew upgrade pyenv to make sure pyenv is up-to-date
  2. $ pyenv install --list
Available versions:
  2.1.3
  2.2.3
  2.3.7
  ...
  3.6.6
  3.6.7
  3.6.8
  3.6.9
  3.6.10
  ...
  3.9.7
  3.10.0 👈
  3.10-dev
  3.11-dev
  activepython-2.7.14
  activepython-3.5.4
  ...

The current latest stable version is 3.10.0 which I’m going for.

$ pyenv install 3.10.0

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.0.tar.xz...
-> https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz
Installing Python-3.10.0...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.0 to /Users/👻/.pyenv/versions/3.10.0

Make Your Python version default

Once you have the version you need (presumably the latest one) installed with pyenv, you need to make sure you are going to use it as a default Python from now on.

1.Set the version you want with pyenv to be global

$ pyenv global <x.x.x> , where <x.x.x> is the version (3.10.0 for me at that time)

2.Make sure your shell (ZSH or Bash) picks it up every time

for ZSH:
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc

for Bash:
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc

3.Check that it works

Open a new terminal tab/window OR reload you current session with $ source ~/.zshrc($ source ~/.bashrc)
and run the following: $ python --version && pip --version

You should see something like:

Python 3.10.0
pip 21.2.3 from /Users/👻/.pyenv/versions/3.10.0/lib/python3.10/site-packages/pip (python 3.10)

Congrats! There you have it!

A couple of wrap-up points

  • Whenever you update your Python3 🐍 with pyenv you will need to change global setting of the version you want to work as a default.
  • You can switch versions when and if you need to and not necessarily keep them as global setting. 👍
Artem Dvornichenko

Artem Dvornichenko