カテゴリー
Mac ブログ 開発

開発 備忘録 pipの使い方

pipとは、Pythonに特化したパッケージ管理システムです。Python Package Index(pypi)とよばれるインデックスを利用して、パッケージをインストールしたり削除したりします。
pipをインストールするには、Pythonが必要となります。まず最初にHomebrewでPythonをインストールしてください。手順はこちら

最初に実行すること help

基本的に、helpにすべてが書かれています。コマンドラインからpip helpと入力します。

$ pip help
Usage:
pip <command> [fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][options]

Commands:
  install                     Install packages.
uninstall                   Uninstall packages.
freeze                      Output installed packages in requirements format.
list                        List installed packages.
show                        Show information about installed packages.
search                      Search PyPI for packages.
wheel                       Build wheels from your requirements.
zip                         DEPRECATED. Zip individual packages.
unzip                       DEPRECATED. Unzip individual packages.
help                        Show help for commands.

General Options:
  -h, –help                  Show help.
–isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, –verbose               Give more output. Option is additive, and can be used up to 3 times.
-V, –version               Show version and exit.
-q, –quiet                 Give less output.
–log <path>                Path to a verbose appending log.
–proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
–retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
–timeout <sec>             Set the socket timeout (default 15 seconds).
–exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
–trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
–cert <path>               Path to alternate CA bundle.
–client-cert <path>        Path to SSL client certificate, a single file containing the private key and the                              certificate in PEM format.
–cache-dir <dir>           Store the cache data in <dir>.
–no-cache-dir              Disable the cache.
–disable-pip-version-check

                              Don’t periodically check PyPI to determine whether a new version of pip is available for                              download. Implied with –no-index.

特に調べたいコマンドがある場合には、名称を入れて、helpを入力します。

$ pip help freeze

pypiを検索

pipは前述したPython Package Index(pypi)にあるパッケージを、searchコマンドで検索します。しかしながら、”Django”等の文字列で検索すると、対象となるファイルが多いため、すべて読み込むまでかなり時間がかかることがありますので、気をつけてください。

$ pip search django-bootstrap-form
django-bootstrap-form     - django-bootstrap-form
  INSTALLED: 3.1 (latest)
django-bootstrap-form-horizontal - Templates for bootstrap3 style horizontal forms in Django
django-bootstrap-forms    - Twitter Bootstrap ready Django forms.

INSTALLEDと出力されていたら、すでにインストールされているという意味になります。–index <url>をつけることによって、pypi以外を検索することができますが、多くがpypiからとなるでしょう。

インストールする

installを使います。

$ pip install django-bootstrap-form
 

現在インストールされているパッケージを表示

listを使います。

$ pip list
Django (1.7.1)
django-bootstrap-form (3.1)
pip (6.0.6)
setuptools (11.0)

ここで、oプションをつけると、更新可能なパッケージを表示してくれます。
-o

更新可能なパッケージを表示
$ pip list -o
Django (Current: 1.7.1 Latest: 1.7.2)
setuptools (Current: 11.0 Latest: 11.3.1)
ここで、uオプションで最新バージョンがインストールされているパッケージを表示します。他にも、以下の様なオプションが用意されています。
-u
最新バージョンが入っているパッケージを表示
-e
install -e で入れたパッケージを表示
-l
virtualenv環境の場合、システムにインストールされているパッケージは表示しない
–pre
ベータバージョンも表示

もう一度インストールする

-I (–ignore-installed)を使います。

$ pip install Django -I

アップグレードしたい

一度インストールしたパッケージをアップグレードする場合には、-U (–upgrade)を使います。

$ pip install Django -U
Collecting Django from https://pypi.python.org/packages/py2.py3/D/Django/Django-1.7.2-py2.py3-none-any.whl#md5=b57f9a2dec214b60e338aa80fb902936
  Downloading Django-1.7.2-py2.py3-none-any.whl (7.4MB)
    100% |################################| 7.4MB 95kB/s 
Installing collected packages: Django
  Found existing installation: Django 1.7.1
    Uninstalling Django-1.7.1:
      Successfully uninstalled Django-1.7.1
Successfully installed Django-1.7.2

上記の通り古いバージョンのアンインストールも行ってくれますので安心です。Djangoは、頻繁にアップデートがありますので、注意してください。

[追記&補&重要な備忘録]

pipを使ったアップグレードについては、

sudo pip3 install –upgrade pip

ってな記述もあるみたいなのですが、失敗することがあるみたいです。上記で紹介した -Uを使った記述でだけ成功する場合もあるようなので、Pyhotn3のpip(またはpip3)からは、-Uの方をつかっていただくといいかもしれません。

 

インストールしたパッケージの詳細やディレクトリを見る

showを使います。

$ pip show django
---
Name: Django
Version: 1.7.2
Location: /Users/hideki/.virtualenvs/env1/lib/python3.4/site-packages
Requires: 

上記のディレクトリを見ると、env1という仮想環境のPython3.4ディレクトリ配下にあることがわかります。-fをつけることによって、パッケージに含まれているすべてのファイルを表示します。

他にも下記のようなものがあります。

特定のバージョンをインストールする

pip install Django==1.7.2

手元のリポジトリから直接インストール

pip install -e .

tar.gzやzipファイルから直接インストール

$ pip install ./downloads/XXX.tar.gz
$ pip install http://xxx.com/xxxx.zip

インストールせずにダウンロードする

$ pip installDjango -d /tmp/

uninstall

$ pip uninstall Django

設定

pipの設定ファイルは、仮想環境かどうかなどによって、場所が変わっています。
こちらのサイトを参照してください。なお、下記ユーザガイドで、半角スペースが含まれたディレクトリへの移動などが必要になる場合には、こちらを参考に。
pipユーザガイド(英語)

[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]