へび。いくつになってもよくやる。

Python。日本よりも海外でよく使われている言語で、アプリケーションのスクリプト制御用にもよく採用されているイメージです。

僕に身近なところでは、おなじみのGoogle Apps Engineや、オフィススイートのLibbreOffice(最近全く使わなくなったけど)、3DCGツールのBrenderなんかがPython対応しているので、その辺からずっと前から気にはなってました。

そんな中、最近使っていてるタスク管理ツール、Todoistで提供されているAPIのドキュメントページがPythonでのコードを見本に載せているのを見た途端、ページのかっこいい見た目、構造に喚起され、ついに触ってみる気になりました。
何がきっかけになるか分からないものですね。

Todoistのドキュメントページのデザインはすごくかっこいい

Blenderとかも、もっと使い込んでみたいツールなので、その辺でも役立ちそうですしね。

とは言え、普通に「$ gem search todoist」とかやると、Ruby用のGemも見つかるので、肌に合わなければ、いつでもそっちに乗り換えるつもりではいます(笑)

インストール環境

  • MacBook Pro (Retina, Mid 2012)
  • OS 10.10.4
  • Zsh 5.0.7
  • Homebrew 0.9.5

Zshを使っているので、標準のBash等をつかっている場合はご注意をば。

Macには最初から一応古いPythonは入っている

ターミナルで確認すると、入ってるね。Python。

$ python -V
Python 2.7.6

Pythonは2.xと3.xとで互換性の面で問題はあるみたいだけど、個人開発なら3だけ触る形でも、問題ないだろうと想像してます。

Pythonのバージョン管理ツール「pyenv-virtualenv」をインストール

Rubyならrbenv、Pythonならpyenv-virtualenvってバージョン切り替えツールがあるんですね。
3.xオンリーで行くつもりとは言え、こういうのは入れておいた方が楽だと思います。

$ brew update
$ brew install pyenv-virtualenv

とりあえずコマンド確認

$ pyenv
pyenv 20150719
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/yyuu/pyenv#readme

rbenvと同じような感覚で伝えそうですね。

最新のPython安定版をインストールしてグローバル化

バージョンを確認。

$ pyenv install -l
Available versions:
  # (省略)
  3.4.3
  # (この下にも、anaconda, ironpython, jython, miniconda, pypy, stacklessとかの別ディストリビューションのバージョンが列挙されてるけど省略。いろいろあるんですね。)

とりあえず一番新しい安定版っぽい3.4.3を入れる事にします。

$ pyenv install 3.4.3

グローバル化して使えるように。

$ pyenv global 3.4.3

ちゃんと入りました

$ pyenv versions
  system
* 3.4.3 (set by /Users/USER_NAME/.pyenv/version)

僕はパスの通し忘れが多い

これで3.4.3になったはず。いざバージョン確認……って、あれ? 2.7.6?

$ python -V
Python 2.7.6

あーあー、パスが通ってないですね。
~/.zsh_profile に追記しましょうか。

pyenvのシェルの準備コマンドも入れないとダメでした。
こっちは ~/.zshrc に入れておくと良さそうです。

こうすれば……

$ echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.zsh_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc

OKですね。

$ python -V
Python 3.4.3

パッケージ管理システム「pip」も入れる

Pythonの定番パッケージ管理システムは「pip」と言うものらしいです。ただ、それを導入するにはpipの下位版的なpythonの標準コマンド「easy_install」を使うそうです。

Pythonのパッケージ管理システムは色々あるけど、今の所pipオンリーで考えて良さそうです。

$ easy_install pip
Searching for pip
Best match: pip 6.0.8
Adding pip 6.0.8 to easy-install.pth file
Installing pip3 script to /Users/USER_NAME/.pyenv/versions/3.4.3/bin
Installing pip3.4 script to /Users/USER_NAME/.pyenv/versions/3.4.3/bin
Installing pip script to /Users/USER_NAME/.pyenv/versions/3.4.3/bin

Using /Users/USER_NAME/.pyenv/versions/3.4.3/lib/python3.4/site-packages
Processing dependencies for pip
Finished processing dependencies for pip

あっさり入りましたね。

$ pip -V
pip 6.0.8 from /Users/USER_NAME/.pyenv/versions/3.4.3/lib/python3.4/site-packages (python 3.4)

これで基本的な環境の準備ができました。
とりあえずHello Worldでexit。

$ python
Python 3.4.3 (default, Aug  8 2015, 19:14:43)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
>>> exit()

本格的にPythonやるならやっぱりオライリー本「入門 Python3」なのかなー。600ページかー(笑)