Install Python and libraries for Python on MacOS Catalina

Introduction

I tried to build an environment when studying Python, but this was unexpectedly difficult.

Python is installed by default on Mac, but old Python2 is installed instead of the latest Python3, so it can not be left as it is for new study & development.

Moreover, there are many installation methods, and it was difficult for Python beginners to know which method to install. Furthermore, if I didn't know the right or left, I was confused with the Python installation method and the library installation method, and there were many occasions when I was asked, "What? What were you doing now?"

I had the same problem when I asked my fellow Python students, so I thought I'd write it as a memorandum.

This time, I installed the latest version of Python 3 and the game library pygame on Mac OS Catalina using Homebrew and pyenv.

Execution environment

Package to use

Installation flow

  1. [Install Homebrew](# 1 Install homebrew)
  2. [Install pyenv](Install # 2pyenv)
  3. [Install python with pyenv](# 3 Install python with pyenv)
  4. [Install library with pip](# 4 Install library with pip)

1. Install Homebrew

Execute the following command in the terminal according to the official documentation. Of course, if it is already installed, you can skip it.

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

If the installation is successful, you can check the version of Homebrew with brew -v.

$ brew -v
Homebrew 2.4.9
Homebrew/homebrew-core (git revision 3b87b; last commit 2020-08-04)
Homebrew/homebrew-cask (git revision 2ee9f; last commit 2020-08-04)

2. Install pyenv

Update Homebrew to the latest version with brew update and install pyenv with brew install.

$ brew update
$ brew install pyenv

It is OK if you can check the version of pyenv with pyenv -v.

$ pyenv -v
pyenv 1.2.20

pyenv itself works with this, but Python installed via pyenv will not be recognized unless you add the pyenv settings to the terminal, so add the settings to the terminal with the following command and make the terminal recognize the added settings.

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

If the terminal is bash, use the following command.

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

Of course, there is no problem even if you write directly to the profile etc. with vim.

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

3. Install Python with pyenv

You can check the version of Python that can be installed by pyenv with the pyenv install -l command.

$ pyenv install -l
Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  ...
  3.8.4
  3.8.5
  3.9.0b5
  3.9-dev
  3.10-dev
  ...

Unless you have a specific reason, it's a good idea to have the latest stable version (the one without the'dev'after the number) installed. By the way, platforms such as anaconda can also be installed with pyenv. This time install 3.8.5 with the pyenv install command.

$ pyenv install 3.8.5

At this stage, the installation is just complete, and the version of Python used has not yet been switched. python -vEven if you check the version with the command, python2 installed by default on mac is recognized.

$ python -V
Python 2.7.16

Switch the installed Python to work by default with the pyenv global command.

$ pyenv global 3.8.5

If you can confirm that the version has been switched with python -V, the installation is successful. If it doesn't recognize it well, try reloading the pyenv settings into the terminal with `` $ source .zshrc```. (* For bash, use $ source .bash_profile`.)

$ python -V
Python 3.8.5

By the way, you can see the list of Python versions installed with ``` pyenv versions` ``. The one with * at the beginning works as the default version.

$ pyenv versions
  system
* 3.8.5 (set by /Users/linus/.pyenv/version)

4. Install the library with pip

Install the library with the pip install command. This time I will install pygame.

$ pip install pygame

This time, we need the latest version under development, not the latest stable version, so specify the version and install it. You can install by specifying the version by adding the version after the library name.

$ pip install pygame==2.0.0.dev10

You can see a list of libraries installed using pip with the pip list command.

$ pip list
Package    Version
---------- -----------
pip        20.1.1
pygame     2.0.0.dev10
setuptools 47.1.0

You can install it with the specified version. Other libraries, such as Django, can be installed in the same way.

bonus

When uninstalling the package etc. installed this time, you can execute it with the following command. Please note that files may remain when uninstalling unless they are deleted in the reverse order of installation.

$ pip uninstall pygame
$ pyenv uninstall 3.8.5
$ brew uninstall pyenv

Afterword

I had a lot of trouble when I was working on building the environment for the first time, but it was quite difficult to reorganize it. I often hear that Python is a language that is easy for beginners to program, but it is hard to say that environment construction is for beginners.

Recommended Posts

Install Python and libraries for Python on MacOS Catalina
Install Python less than 3.5.3 on macOS Catalina
Install Python 3 on MacOS Catalina (with Homebrew only)
Clean install and work environment update for macOS Catalina
Compile and install MySQL-python for python2.7 on amazon linux
Install confluent-kafka for Python on Ubuntu
6 Python libraries for faster development and debugging
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Install and run Python3.5 + NumPy + SciPy on Windows 10
Install Python on Pidora.
Install Scrapy on python3
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Install Python on Mac
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
How to install MeCab (v0.996) and libraries for Python without administrator privileges
Install python on windows
Run pip install on MacOS Python 3.7 or later
Install Python 3.4 on Mac
Install MongoDB on Ubuntu 16.04 and operate via python
Install Python (for Windows)
Install ZIP version Python and pip on Windows 10
Initial settings for using Python3.8 and pip on CentOS8
Install Python3 on Mac and build environment [Definitive Edition]
Install psycopg2 (pgsql library for python3) on Apple Silicon
List of Python libraries for data scientists and data engineers
Install selenium on Mac and try it with python
Notes on creating a python development environment on macOS Catalina
[Windows] [Python3] Install python3 and Jupyter Notebook (formerly ipython notebook) on Windows
Install pyenv on Raspberry Pi and version control Python
Install django on python + anaconda and start the server
Set up python and machine learning libraries on Ubuntu
For those who can't install Python on Windows XP
Install watchdog on Windows + Python 3.3
Python on Ruby and angry Ruby on Python
Install pygame on python3.4 on mac
Install Python 3.8 on CentOS 7 (SCL)
OpenCV3 installation for Python3 @macOS
Made python available on macOS
Install pandas 0.14 on python3.4 [on Mac]
Install Python 3.7 and Django 3.0 (CentOS)
Install OpenCV on Ubuntu + python
Install Python 3.8 on CentOS 8 (AppStream)
Catalina on Mac and pyenv
[Python] [Chainer] [Windows] Install Chainer on Windows
Run OpenVino on macOS Catalina
Install dlib for Python (Windows)
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Use Python 3 introduced with command line tools on macOS Catalina
Install python3 and scientific calculation library on Ubuntu (virtualenv + pip)
IP spoof using tor on macOS and check with python
Install pyenv on MacBook Air and switch python to use
Python 3.4 series is safe for installing and executing Python Kivy (macOS)
Install python and Visual Studio Code on windows10 (April 2020 version)
Install Python Pillow on Amazon Linux
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Notes on Python and dictionary types
Python 3.6 on Windows ... and to Xamarin.
Install Cheminformatics Tool RDKit for Python
Install Python3 on Sakura server (FreeBSD)