MacでPythonを始めようと思ったときにやったこと
この記事は、以下の記事を参考にやった作業をまとめたメモです(環境構築しただけです)。 http://qiita.com/spyc/items/73d1295f8b3dde3b49ca
次の職場ではPythonをガッツリ使うことになりそうなので、公式ドキュメントを見ながらゆったり体系的に勉強する機会を作っても良いかなぁと思った次第。 人から貰ったPythonスクリプトを実行した記憶があったので、実行環境は整っていたはず。 Pythonには2系と3系が存在することも薄っすらと認識していて、どうせなら3系をつかいたなぁと。←と現在この程度のPython力です。
環境
まずは環境
$ sw_vers ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1212 $ brew -v Homebrew 1.2.2 Homebrew/homebrew-core (git revision 80032; last commit 2017-06-09)
確かPython2系は入っていた気がするが…
$ python Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
ということで、どうやら2.7系は入っているらしい。
pyenv(バージョン管理ツール)の導入
Node.jsで言うところのnodebrewとイメージすればよいだろうか。 pyenvというのをインストールしました。今回はbrew経由で。
brew install pyenv ==> Installing dependencies for pyenv: openssl ==> Installing pyenv dependency: openssl ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2l.el_capitan.bottle.tar.gz ######################################################################## 100.0% ==> Pouring openssl-1.0.2l.el_capitan.bottle.tar.gz ==> Using the sandbox ==> Caveats A CA file has been bootstrapped using certificates from the SystemRoots keychain. To add additional certificates (e.g. the certificates added in the System keychain), place .pem files in /usr/local/etc/openssl/certs and run /usr/local/opt/openssl/bin/c_rehash This formula is keg-only, which means it was not symlinked into /usr/local, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/openssl/lib CPPFLAGS: -I/usr/local/opt/openssl/include For pkg-config to find this software you may need to set: PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig ==> Summary 🍺 /usr/local/Cellar/openssl/1.0.2l: 1,709 files, 12.1MB ==> Installing pyenv ==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.0.10_1.el_capitan.bottle.tar.gz ######################################################################## 100.0% ==> Pouring pyenv-1.0.10_1.el_capitan.bottle.tar.gz 🍺 /usr/local/Cellar/pyenv/1.0.10_1: 563 files, 2.2MB
パスを設定する。 .bash_profileに以下を追記
export PYENV_ROOT="${HOME}/.pyenv" export PATH="${PYENV_ROOT}/bin:$PATH" eval "$(pyenv init -)"
ターミナルを再起動してpyenvコマンドが使えることを確認する。
$ pyenv pyenv 1.0.10 Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versions List all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help <command>' for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme
Python3のインストール
Pythonの公式ドキュメントを見ると、現時点の最新は3.6.1らしい。
$ pyenv install 3.6.1 Downloading Python-3.6.1.tgz... -> https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz Installing Python-3.6.1... Installed Python-3.6.1 to /Users/d_ojima/.pyenv/versions/3.6.1
次のコマンドでpyenvでインストールしたバージョンの一覧が確認できる。
$ pyenv versions * system (set by /Users/d_ojima/.pyenv/version) 3.6.1
バージョンを切り替えます。
$ pyenv global 3.6.1 $ pyenv rehash
切り替わっているかの確認をします。
$ pyenv versions system * 3.6.1 (set by /Users/d_ojima/.pyenv/version)
$ python --version Python 3.6.1
pyenv優秀
もともと入っていた2.7系もsystemという名前でpyenvから切り替えられるのみたいです。
pipのインストール
Node.jsで言うところのnpmだと信じておく。 Pythonの標準的なパッケージ管理ツール。
$ sudo easy_install pip Searching for pip Best match: pip 9.0.1 Adding pip 9.0.1 to easy-install.pth file Installing pip script to /Users/d_ojima/.pyenv/versions/3.6.1/bin Installing pip3 script to /Users/d_ojima/.pyenv/versions/3.6.1/bin Installing pip3.5 script to /Users/d_ojima/.pyenv/versions/3.6.1/bin Using /Users/d_ojima/.pyenv/versions/3.6.1/lib/python3.6/site-packages Processing dependencies for pip Finished processing dependencies for pip
pipが使えるか確認。
$ pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. pip (9.0.1) setuptools (28.8.0)
pipコマンド自体は通るが、何やら赤文字でDEPRECATIONが表示されている。
毎回この表示は流石にうざいので、素直に従ってみます。
次の記事を参考にしました。
pip.confという設定ファイルに出力フォーマットの設定をすればいよことがわかったので。
$ cd ~/Library/'Application Support' $ mkdir pip && cd pip $ touch pip.conf
作成したpip.confに以下を記述
[list] format = legacy
再度確認してみます。
$ pip list pip (9.0.1) setuptools (28.8.0)
警告が表示されなくなりました。 めでたし。
雑感
環境構築的な作業は久々にやった気がする。
毎回思うけど、リファレンス通りにやってもバージョン違いなどが原因で必ずと言っていいほど一筋縄には行かない印象。
これからどんどんPython書くどん!