Python モジュールのインストール

Email:distutils-sig@python.org

人気のあるオープンソース開発プロジェクトがそうであるように、Python には貢献者たちとユーザたちの活発なサポートコミュニティがあり、またこれらはほかの Python 開発者たちに、彼らのソフトウェアのオープンソースライセンスのもとでの利用も可能にしてくれています。

これはほかの人が既に挙げた共通の(あるいは時折極めて稀有な!)問題や、彼ら自身の解法による潜在的な貢献が共通の場所に蓄えられることによる恩恵によって、Python ユーザに共有と協調を効果的に行なうことの助けとなっています。

このガイドはこれらプロセスのうち、インストールについてをカバーします。あなた自身の Python プロジェクトを作成し、シェアするためのガイドについては distribution guide を参照してください。

注釈

あなたが企業や組織のユーザであれば、多くの組織がオープンソースソフトウェアの利用と貢献に関する彼ら独自のポリシーを持っていることに気をつけてください。Python によって提供される配布とインストールのツールを利用する際には、そのようなポリシーを考慮に入れてください。

重要用語集

  • pip は推奨されるインストーラ・プログラムです。 Python 3.4 からは、 Python バイナリ・インストーラに最初から付属するようになりました。
  • a virtual environment is a semi-isolated Python environment that allows packages to be installed for use by a particular application, rather than being installed system wide
  • pyvenv is the standard tool for creating virtual environments, and has been part of Python since Python 3.3. Starting with Python 3.4, it defaults to installing pip into all created virtual environments
  • virtualenv is a third party alternative (and predecessor) to pyvenv. It allows virtual environments to be used on versions of Python prior to 3.4, which either don't provide pyvenv at all, or aren't able to automatically install pip into created environments.
  • the Python Packaging Index is a public repository of open source licensed packages made available for use by other Python users
  • the Python Packaging Authority are the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. They maintain a variety of tools, documentation and issue trackers on both GitHub and BitBucket.
  • distutils はオリジナルのビルド・配布システムで、 Python 標準ライブラリに 1998 年に最初に追加されました。 distutils の直接的な利用は段階的に取り払われていきますが、それは今でも現時点でのパッケージングと配布のインフラストラクチャの基礎として鎮座していて、標準ライブラリの一部として残っているだけでなく、その名前はほかの文脈でも生き続けています(Python のパッケージング標準の開発をまとめるのに使われているメーリングリストの名前のように)。

基本的な使い方

パッケージングのための標準ツールはすべてコマンドラインから使われることを想定しています。

以下のコマンドは、モジュールの最新バージョンとそれが依存するモジュールを、 Python Packaging Index からインストールします。

python -m pip install SomePackage

注釈

POSIX ユーザ(Max OS X と Linux ユーザを含みます)向けには、このガイド内の例は、 virtual environment の利用を前提にしています。

Windows ユーザ向けには、このガイド内の例は、Python インストール時にシステムの PATH 環境変数が調整されていることを前提にしています。

正確なバージョンや最小のバージョンをコマンドライン上で直接指定することもできます。>< などの比較演算子など、シェルが解釈する特殊文字を使う場合、パッケージ名とバージョンを二重引用符で囲んでください:

python -m pip install SomePackage==1.0.4    # specific version
python -m pip install "SomePackage>=1.0.4"  # minimum version

通常、適合するモジュールがインストール済である場合にそれを再度 install 実行を試みても効果はありません。既に存在しているモジュールのアップグレードには、明示的にそれを要求しなければなりません:

python -m pip install --upgrade SomePackage

pip とその機能についての詳しい情報とリソースは Python Packaging User Guide にあります。

pyvenv has its own documentation at pyvenv - Creating virtual environments. Installing into an active virtual environment uses the commands shown above.

どうすればいいの...?

以下はよくある課題への簡単な回答もしくは回答へのリンクです。

... pip を 3.4 より前のバージョンの Python でインストールするには?

pip が Python に付属するのは 3.4 以降です。それ以前のバージョンでは、 "Python Packaging User Guide" の記載にしたがって pip 自体をインストールする必要があります。

... パッケージを現在のユーザ用のみにインストールするには?

python -m pip install--user オプションを付けてください。パッケージはシステムのすべてのユーザ用にではなく、現在のユーザ用のみにインストールされます。

... 科学技術計算用の Python パッケージをインストールするには?

A number of scientific Python packages have complex binary dependencies, and aren't currently easy to install using pip directly. At this point in time, it will often be easier for users to install these packages by other means rather than attempting to install them with pip.

... インストールされた複数のバージョンの Python を並行して使うには?

On Linux, Mac OS X and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

(appropriately versioned pip commands may also be available)

Windows では、 Python ランチャーの py-m スイッチとの組み合わせで使ってください。

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

よくあるインストールに関する問題

Linux で、システムの Python 内にインストールする

Linux システムでは、 Python のインストールは典型的には linux ディストリビューションの一部に含まれています。この Python インストールへのインストールには、システムに対する root 権限が必要で、また、 pip によって期待されているものとは異なるコンポーネントにアップグレードすることで、システムのパッケージマネージャのオペレーションやシステムのほかのコンポーネントの邪魔をするかもしれません。

そのようなシステムでは、 pip でパッケージをインストールする際には仮想環境を使うか、ユーザごとのインストールを行うのが、大抵良い方法です。

バイナリの拡張のインストール

Python プロジェクトの多くはソースに基いた配布に強く依存しており、インストールプロセスの一環として、エンドユーザ環境でソースから拡張モジュールをコンパイルすることを期待します。

バイナリ wheel フォーマットのサポートが導入されたことで、また、少なくとも Windows と Mac OS X についての wheels の公開が Python Packaging Index を通して出来るようになったことで、この問題についての開発者の時間の節約と、ユーザにとっては自身でビルドせずにビルド済み拡張をインストールするさらなる標準化に繋がるかもしれません。

Some of the solutions for installing scientific software that is not yet available as pre-built wheel files may also help with obtaining other binary extensions without needing to build them locally.