量産メモ帳

忘れっぽいのでメモを残しています。浅く適当に書きます。

Pythonをインストールする。

スポンサーリンク

今年に入って急に色んな場面でPythonに関わることになったのですが、未経験の言語なので、最初のインストール段階で色々と躓いたり疑問に感じることが出てきました。
後で振り返って検証ができるように、開発環境を構築した時のメモを残しておきます。

バージョン

まず、そもそもインストールするPythonのバージョンを決める必要があります。
現時点での最新版はPython3.7.2です。
しかし、世間ではまだPython2系のアプリケーションが数多く残っているみたいで、自分が関与したプロジェクトもそうでした。
別にPython2系でも良いかなと思ったのですが、Python経験者に話を聞いたところ、どうやらJavaとは異なりPythonの2系と3系は互換性が乏しいみたいです。
こういうサイトを見ると、確かに違いは多そうですね。
python-future.org
自分自身で一から作り上げるプロジェクトは3系をデフォルト選択するつもりですが、既存プロジェクトを動かすためには2系が必要になるかなと思いました。
従って結局のところ、2系と3系の両方をインストールすることにしました。

インストール

インストーラ

Windowsを使っていれば、公式サイトが用意するインストーラを使ってインストールするの一択しかないと思います。
けれどもMacを使用していると、公式のインストーラを直接ダウンロードする以外に、HomebrewというMac用のパッケージ管理ソフトを使ってインストールする選択肢もあります。
で、詳しい理由は後述しますが、今回は公式のインストーラは使わず、Homebrewでのインストールを選択しました。

Homebrew

パッケージ管理ソフトを使う利点は、インストールだけでなくバージョンアップやアンインストールが簡単にできるということです。
Homebrewは以下のコマンドでインストールできます。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

次に以下のコマンドを打つだけでPython3系をインストールできます。

brew install python

ちなみにPython2系をインストールする場合のコマンドは以下の通りです。

brew install python@2

インストールしたバージョンの確認は brew info コマンドでもできますが、パスが通っているかを確認するためにも、直接、pythonのコマンドを叩いた方が良いでしょうね。

python2 --version

Python 2.7.15

python3 --version

Python 2.7.15

ところで2系と3系の両方をHomebrewでインストールしてみて気付いたのですが、末尾に数字が付かないpythonコマンドは2系にリンクしています。

cd /usr/local/bin/
ls -l python* | awk '{print $9,$10,$11}'

python -> ../Cellar/python@2/2.7.15_3/bin/python
python-config -> ../Cellar/python@2/2.7.15_3/bin/python-config
python2 -> ../Cellar/python@2/2.7.15_3/bin/python2
python2-config -> ../Cellar/python@2/2.7.15_3/bin/python2-config
python2.7 -> ../Cellar/python@2/2.7.15_3/bin/python2.7
python2.7-config -> ../Cellar/python@2/2.7.15_3/bin/python2.7-config
python3 -> ../Cellar/python/3.7.2_2/bin/python3
python3-config -> ../Cellar/python/3.7.2_2/bin/python3-config
python3.7 -> ../Cellar/python/3.7.2_2/bin/python3.7
python3.7-config -> ../Cellar/python/3.7.2_2/bin/python3.7-config
python3.7m -> ../Cellar/python/3.7.2_2/bin/python3.7m
python3.7m-config -> ../Cellar/python/3.7.2_2/bin/python3.7m-config
pythonw -> ../Cellar/python@2/2.7.15_3/bin/pythonw
pythonw2 -> ../Cellar/python@2/2.7.15_3/bin/pythonw2
pythonw2.7 -> ../Cellar/python@2/2.7.15_3/bin/pythonw2.7

なので、意識して3系を使いたい時は、pythonではなくpython3と打つ必要があります。

pyenv

Homebrewで一通りインストールした後にpyenvの存在を思い出しました。
思い出したというのは、Go言語にもgoenvというものがあるからです。
rms-099.hatenablog.jp
おそらくpyenvの方が同じ3系でも異なる複数のバージョンをインストールすることができると思いますが、今の所、そこまでの細かさは必要ないので、今回は使用しません。
…と書きましたが、結局、pyenvを使った方が楽だなという結論に達しました。
Macならば、こんな感じで初期設定ができるんじゃないでしょうか?

brew update
brew install pyenv
echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bash_profile
echo 'export PATH="${PYENV_ROOT}/bin:${PATH}"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

そして、主に以下のコマンドの使い方を覚えておけば、とりあえずは大丈夫なんじゃないかと。

  • pyenv install --list
  • pyenv install (version)
  • pyenv global (version)
  • pyenv local (version)

ちなみに自分のMacで試してみたら、いきなりインストールで失敗しました。

pyenv install 3.7.2

python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.2.tar.xz...

Installing Python-3.7.2...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.14.3 using python-build 20180424)
:(中略)
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

zlibが無いと言うから brew install zlib でzlibをインストールして、再び pyenv install 3.7.2 しても駄目でした。
どうやらmacOS_SDK_headersというものが入っていないことが原因だそうです。

なので、まずは以下のコマンドを実行します。*1

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

installer: Package name is macOS_SDK_headers_for_macOS_10.14
installer: Installing at base path /
installer: The install was successful.

コマンドが成功したら、再びpyenvでインストールします。

pyenv install 3.7.2

python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.2.tar.xz...

Installing Python-3.7.2...
python-build: use readline from homebrew
Installed Python-3.7.2 to ~/.pyenv/versions/3.7.2

最後に以下のコマンドを実行すれば、指定したバージョンのPython実行ファイルにパスが通ります。

pyenv global 3.7.2
python --version

Python 3.7.2

Python用パッケージマネージャ

HomebrewはMac用のパッケージ管理ソフトだけど、Python用のパッケージ管理ソフトというものもあります。
詳しくは調べていませんが、pipやsetuptoolsあたりが代表的っぽいです。

pip

今まで関わってきたPythonプロジェクトを見ると、とりあえずpipがあれば間に合いそうです。
昔はそうではなかったみたいですが、今ではPythonをインストールするとpipも一緒にインストールされます。
ただ、ここでもPythonの2系と3系の違いが関わってきて、単にpipを打ってしまうと、Python2系のパッケージをインストールしてしまうことになります。

cd /usr/local/bin/
ls -l pip* | awk '{print $9,$10,$11}'

pip -> ../Cellar/python@2/2.7.15_3/bin/pip
pip2 -> ../Cellar/python@2/2.7.15_3/bin/pip2
pip2.7 -> ../Cellar/python@2/2.7.15_3/bin/pip2.7
pip3 -> ../Cellar/python/3.7.2_2/bin/pip3
pip3.7 -> ../Cellar/python/3.7.2_2/bin/pip3.7

GitHubなどにあるPythonプロジェクトのREADMEとかにpipを利用するように書かれていたら、そのプロジェクトはおそらく2系でしょう。
↑HomeBrewでpythonを直接インストールした場合の話でした。
pyenv経由でインストールすると、pipのバージョンは pyenv global で指定したものになります。

pip --version

pip 18.1 from ~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/pip (python 3.7)

なお、pip自身をアップグレードすることもできます。

pip install --upgrade pip

Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)

Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3

最新の19.0.3になりました。

pip --version

pip 19.0.3 from ~/.pyenv/versions/3.7.2/lib/python3.7/site-packages/pip (python 3.7)

setuptools

こちらは必要になったらインストールすることにします。

統合開発環境(IDE)

こちらのサイトに一覧が載っていますけど、何がベストなのか全く分かりません。

ただ、他のプログラミング言語でも開発している人なら、言語ごとにIDEを切り替えるのは面倒なので、無償版ならEclipse(PyDev)かNetBeansVSCodeあたりになってくるんじゃないでしょうか。
で、有償版なら最近勢いのあるJetBrainsPyCharmあたりかなと。
とりあえず自分はGo言語でも使っているVSCodeにしました。

その他

Pylint

VSCodePythonソースコードファイルを開いたら、Pylintのインストールを求められました。
名前的におそらくPythonソースコードの静的チェックツールだと思いますが、VSCodeの指示に従えば、VSCodeが以下のコマンドを実行してPylintをインストールしてくれます。

/usr/local/opt/python@2/bin/python2.7 -m pip install -U "pylint<2.0.0" --user

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Collecting pylint<2.0.0
Downloading https://files.pythonhosted.org/packages/59/65/adcffa71fe942313c4d9e9284565d9a9e67798f4771f464e1d5dd58fea88/pylint-1.9.4-py2.py3-none-any.whl (689kB)
Collecting singledispatch; python_version < "3.4" (from pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/c5/10/369f50bcd4621b263927b0a1519987a04383d4a98fb10438042ad410cf88/singledispatch-3.4.0.3-py2.py3-none-any.whl
Collecting isort>=4.2.5 (from pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/72/41/5188c1dafe27263005a017d4a2dd0def7c97fddc443564a260da414ee94d/isort-4.3.9-py2.py3-none-any.whl (47kB)
Collecting configparser; python_version == "2.7" (from pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/dd/6a/d90db58ec96161e4cae7ced68310f0c5ca860352d35b8ed39f890df15632/configparser-3.7.3-py2.py3-none-any.whl
Collecting backports.functools-lru-cache; python_version == "2.7" (from pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/03/8e/2424c0e65c4a066e28f539364deee49b6451f8fcd4f718fefa50cc3dcf48/backports.functools_lru_cache-1.5-py2.py3-none-any.whl
Collecting mccabe (from pylint<2.0.0)
Using cached https://files.pythonhosted.org/packages/87/89/479dc97e18549e21354893e4ee4ef36db1d237534982482c3681ee6e7b57/mccabe-0.6.1-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: six in /usr/local/Cellar/protobuf/3.6.1.3_1/libexec/lib/python2.7/site-packages (from pylint<2.0.0) (1.11.0)
Collecting astroid<2.0,>=1.6 (from pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/0e/9b/18b08991c8c6aaa827faf394f4468b8fee41db1f73aa5157f9f5fb2e69c3/astroid-1.6.5-py2.py3-none-any.whl (293kB)
Collecting futures; python_version < "3.2" (from isort>=4.2.5->pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/2d/99/b2c4e9d5a30f6471e410a146232b4118e697fa3ffc06d6a65efde84debd0/futures-3.2.0-py2-none-any.whl
Collecting enum34>=1.1.3; python_version < "3.4" (from astroid<2.0,>=1.6->pylint<2.0.0)
Downloading https://files.pythonhosted.org/packages/c5/db/e56e6b4bbac7c4a06de1c50de6fe1ef3810018ae11732a50f15f62c7d050/enum34-1.1.6-py2-none-any.whl
Collecting wrapt (from astroid<2.0,>=1.6->pylint<2.0.0)
Using cached https://files.pythonhosted.org/packages/67/b2/0f71ca90b0ade7fad27e3d20327c996c6252a2ffe88f50a95bba7434eda9/wrapt-1.11.1.tar.gz
Collecting lazy-object-proxy (from astroid<2.0,>=1.6->pylint<2.0.0)
Using cached https://files.pythonhosted.org/packages/55/08/23c0753599bdec1aec273e322f277c4e875150325f565017f6280549f554/lazy-object-proxy-1.3.1.tar.gz
Building wheels for collected packages: wrapt, lazy-object-proxy
Building wheel for wrapt (setup.py) ... done
Stored in directory: ~/Library/Caches/pip/wheels/89/67/41/63cbf0f6ac0a6156588b9587be4db5565f8c6d8ccef98202fc
Building wheel for lazy-object-proxy (setup.py) ... done
Stored in directory: ~/Library/Caches/pip/wheels/a0/63/e2/6d93295282cb35b53b14b50b602c76dfb04471e21b31d8ad7b
Successfully built wrapt lazy-object-proxy
Installing collected packages: singledispatch, futures, isort, configparser, backports.functools-lru-cache, mccabe, enum34, wrapt, lazy-object-proxy, astroid, pylint
The script isort is installed in '~/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
The scripts epylint, pylint, pyreverse and symilar are installed in '~/Library/Python/2.7/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed astroid-1.6.5 backports.functools-lru-cache-1.5 configparser-3.7.3 enum34-1.1.6 futures-3.2.0 isort-4.3.9 lazy-object-proxy-1.3.1 mccabe-0.6.1 pylint-1.9.4 singledispatch-3.4.0.3 wrapt-1.11.1

でもこれだとインストールされるは2系なので、3系のPylintをインストールしたい場合は、自分で以下のコマンドを実行する必要があります。

pip3 install pylint

*1:pkgファイルは各々の環境に存在するものを指定します。