0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MacでのPythonインストール[2019年版]

Last updated at Posted at 2019-05-25

NewPCを手に入れて、環境構築し直したついでで、Mac環境下でのpythonのインストール方法を説明します。

過去の記事ではエラーが発生する部分があり、記事を書きました。

XCODEのインストール

 以下のコマンドでインストールできます。AppStoreからインストールしても特に問題ありません。
('#'はコメントアウトです。実際には無視してください。)

# xcode のインストール
$ xcode-select --install

Homebrewのインストール

すでにインストールしている方は問題ありません。

まだインストールしていない方はHomebrewの公式サイトを参照にインストールしてください。

# Homebreのインストール
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# インストールの確認
$ brew doctor

pyenv のインストール

pyenvはpythonのバージョン管理ツールです。
お使いのシェルがbashの場合以下で実行できます。

$ brew pyenv install
$ cat << 'EOS' >> ~/.bash_profile
export PYENV_ROOT=/usr/local/var/pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
EOS
$ source ~/.bash_profile
$ pyenv --version

私はShellがfishなので、以下のようにしました。

$ vim ~/.config/fish/.config_fish
############ ~/.config/fish/.config_fishに以下の2文を適当なところに挿入。
set PATH $HOME/.pyenv/shims $PATH
eval (pyenv init - | source)
############

# 変更を反映する。
source ~/.config/fish/.config_fish

Vimの使い方に関してはこちらを参照してください。

Pythonのインストール

pyenv install -lで確認し、最新のバージョンをインストールするといいかと思います。

# インストールできるPythonの一覧を確認する
$ pyenv install -l

# 3.7.3をインストールする
$ pyenv install 3.7.3

# いま使っているPythonのバージョンを確認する
$ pyenv versions

# いまインストールした3.7.3を使うように設定する
$ pyenv global 3.7.3

# 確認
$ pyenv versions
$ python

補足

新しいバージョンのpythonをインストールする場合、以下のようなエラーが起きることがあります。

$ pyenv install 3.7.0
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
Installing Python-3.7.0...
python-build: use readline from homebrew
^C
BUILD FAILED (OS X 10.14.5 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/y5/2w1v0zm51476s61mkkt2f1gjrmbh7c/T/python-build.20190525155211.97757
Results logged to /var/folders/y5/2w1v0zm51476s61mkkt2f1gjrmbh7c/T/python-build.20190525155211.97757.log

他の記事ではxcodeのインスールをするように勧めているのですが、お使いのMacがMojaveあるいは10.14より新しいものだとエラーが改善しません。
その場合以下を実行してください。

$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

$ brew install readline xz

以下のような表示が出ればPythonインストール完了です。お疲れ様でした。

$ python
Python 3.7.3 (default, May 25 2019, 15:59:37)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

参照
https://github.com/pyenv/pyenv/wiki/Common-build-problems#build-failed-error-the-python-zlib-extension-was-not-compiled-missing-the-zlib

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?