After updating to MacOS Catalina, install Xcode Command Line Tools and change from Python 2.7 series to 3.7 series (bash)

After upgrading from MacOS Mojave to Catalina, numpy can no longer be imported. While improving this situation, at the same time, the Python 2 series was upgraded to 3.7.7 at this opportunity, although support will end in January 2020. I will describe how to do it as a memorandum in this article.

Error message indicating that numpy cannot be imported

$ python abc.py
Traceback (most recent call last):
  File "abc.py", line 3, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

Error trying to install Python

So I tried to install Python 3.7.7, but I got an error.


$ pyenv install 3.7.7
(Omitted)
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

I found a similar case study on different versions of the OS in another Qiita article. Corrective action for "xcrun: error: invalid active developer path (/ Library / Developer / CommandLineTools) ..." after updating macOS

Install Xcode Command Line Tools

I installed it because I need Xcode Command Line Tools. In my environment, I was able to proceed in the following way.

$ softwareupdate --list
Software Update Tool

Finding available software
Software Update found the following new or updated software:
* Label: Command Line Tools for Xcode-12.1
	Title: Command Line Tools for Xcode, Version: 12.1, Size: 431272K, Recommended: YES, 

$ softwareupdate --install -a
Software Update Tool

Finding available software

Downloading Command Line Tools for Xcode
Downloaded Command Line Tools for Xcode
Installing Command Line Tools for Xcode
Done with Command Line Tools for Xcode
Done.

Check the version of pyenv and the contents of .bash_profile

At this time, the version of pyenv is as follows.

$ pyenv --version
pyenv 1.2.17

The .bash_profile contained the following description:

(Omitted)
PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
     export PATH=${PYENV_ROOT}/bin:$PATH
     eval "$(pyenv init -)"
fi
(Omitted)

Install Python 3.7.7

I tried installing Python 3.7.7 again, but this time the installation is complete.

$ pyenv install 3.7.7
python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.7.tar.xz...
-> https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tar.xz
Installing Python-3.7.7...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.7 to /Users/ecru/.pyenv/versions/3.7.7

Check the version of Python installed by pyenv. Then set 3.7.7 to global.

$ pyenv versions
  system
* 2.7.17 (set by /Users/ecru/.pyenv/version)
  3.7.7
$ pyenv global 3.7.7 
$ python -V
Python 3.7.7

Now the error message stating that numpy can't be imported is gone and abc.py can now be run in the terminal.

That's all for the memorandum.

Recommended Posts

After updating to MacOS Catalina, install Xcode Command Line Tools and change from Python 2.7 series to 3.7 series (bash)
Use Python 3 introduced with command line tools on macOS Catalina
[EC2] How to install and download chromedriver from the command line
Install Python and libraries for Python on MacOS Catalina
python2 series / 3 series, character code and print statement / command line
Allow brew install of command line tools made in Python
How to get a string from a command line argument in python
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
Did not change from Python 2 to 3
Get options in Python from both JSON files and command line arguments
From Python to using MeCab (and CaboCha)
How to install and use pandas_datareader [Python]
Porting and modifying doublet-solver from python2 to python3.
Install Python less than 3.5.3 on macOS Catalina
[Amazon Linux] Switching from Python 2 series to Python 3 series
If you try to install Python2 pip after installing Python3 pip and it is rejected