Mac OS XにPythonをインストールする¶
Mac OS Xの最新バージョンのLionには、Python 2.7が最初から入っているので、すぐに使うことができます。
Pythonを使うためにインストールや何かを設定するといったことは必要ありません。 そうは言っても、実際にPythonアプリケーションを作り始める前に次の章で紹介するツールやライブラリをインストールすることを強くお勧めしています。 特に、他のサードパーティのPythonライブラリを使うのがとても簡単になるので、 Distributeをインストールしておいて下さい。
OS Xに最初から入っているPythonのバージョンは学習するにはいいですが、開発するにはよくありません。 少し古くて、Appleによって修正されているので分からないバグがあるかもしれません。
正しい方法でやりましょう¶
Pythonの正しいバージョンをインストールしましょう。
最初にPythonをコンパイルするためにインストールされているGCCが必要です。 XCode や、より小さな OSX-GCC-Installer というパッケージからインストールすることができます。
Lionには数多くのUNIXユーティリティが入っているので、 Linuxのシステムと同じようにあるコンポーネントがないということをパッケージ管理ソフトが認識します。 Homebrew がこの穴を埋めます。
Homebrewをインストールする ことは簡単です。
$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
それから、 PATH
という環境変数の一番上にHomebrewのディレクトリを追加して下さい。
~/.bashrc
ファイルの下に以下の行を追加するだけです。
export PATH=/usr/local/bin:$PATH
そして、Python 2.7がインストールできました。
$ brew install python --framework
これは、1~2分かかります。
終了したら新しいPythonのスクリプトのディレクトリを PATH
に追加して下さい。
export PATH=/usr/local/share/python:$PATH
--framework
オプションはUNIXスタイルではなく、
フレームワークスタイルでPythonをコンパイルするようにHomebrewに教えます。
Snow Leopardにフレームワークとしてビルドされて入っているPythonはバージョンが古くなっているので、
いくつかのfutureモジュールのインストールの際のバグを避けるようにしています。
Distribute & Pip¶
The most crucial third-party Python software of all is Distribute, which extends the packaging and installation facilities provided by the distutils in the standard library. Once you add Distribute to your Python system you can download and install any compliant Python software product with a single command. It also enables you to add this network installation capability to your own Python software with very little work.
HomebrewにはすでにDistributeがインストールされています。
easy_install
コマンドは、廃止されるとほとんどの人が考えていて、
代わりに pip というのをインストールしています。
Pipはパッケージのアンインストールができ、easy_installとは異なり、活発にメンテナンスされています。
Pipをインストールするには、コマンドプロンプトを開いて以下を実行するだけです。
$ easy_install pip
Virtualenv¶
DistributeとPipを入れた後、次にインストールしたほうがいい開発ツールは、 virtualenv です。 Pipを使ってインストールします。
$ pip install virtualenv
The virtualenv kit provides the ability to create virtual Python environments that do not interfere with either each other, or the main Python installation. If you install virtualenv before you begin coding then you can get into the habit of using it to create completely clean Python environments for each project. This is particularly important for Web development, where each framework and application will have many dependencies.
To set up a new Python environment, change the working directory to where ever you want to store the environment, and run the virtualenv utility in your project’s directory
$ virtualenv --distribute venv
To use an environment, run source venv/bin/activate
. Your command prompt
will change to show the active environment. Once you have finished working in
the current virtual environment, run deactivate
to restore your settings
to normal.
Each new environment automatically includes a copy of pip
, so that you can
setup the third-party libraries and tools that you want to use in that
environment. Put your own code within a subdirectory of the environment,
however you wish. When you no longer need a particular environment, simply
copy your code out of it, and then delete the main directory for the environment.
このページは、 別のガイドライン を書きなおしたもので、 同じライセンスのもとで公開しています。