LoginSignup
2
13

More than 3 years have passed since last update.

pipって何、どう使うの?

Posted at

pipってなに?

pipはpythonのパッケージマネージャの一つです、他にはcondaやpipenvなどがあります。
python3系ではバージョン3.4以降であれば、pythonのインストールと共にpipもインストールされます。
pythonの標準ライブラリに含まれないパッケージのインストールや管理をすることができます。

仮想環境でpipの使い方を見ていきましょう

pipを使えるかチェック:

$ pip --version
pip 10.0.1 from /Users/場所/venv/lib/python3.7/site-packages/pip (python 3.7)

pipのアップデート

$ pip install --upgrade pip

pipのバージョン再チェック

$ pip --version
pip 19.3.1 from /Users/場所/venv/lib/python3.7/site-packages/pip (python 3.7)

pipで使えるコマンド達を見てみましょう:

$ pip help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  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. Option is
                              additive, and can be used up to 3
                              times (corresponding to WARNING,
                              ERROR, and CRITICAL logging
                              levels).
  --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, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair 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.
  --no-color                  Suppress colored output

パッケージをpipでインストール

pythonの標準ライブラリは充実していますが、それ以外にも世界中の開発者が製作したpythonのフレームワーク、ツール、ライブラリなどがPython Package Index(PyPI)にて公開されています。(パイピーアイと読みます、パイパイではないです。)
現在の環境にインストールされているパッケージをpip listでチェック(まだ何もインストールしていない状態):

$ pip list
Package    Version
---------- -------
pip        19.3.1
setuptools 39.0.1

PyPIから使いたいパッケージをインストールするためにはpip installコマンドを使います。
機械学習ライブラリscikit-learnを使いたいとなったら:

$ pip install scikit-learn
Collecting scikit-learn
  Downloading https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl (11.0MB)
     |████████████████████████████████| 11.0MB 851kB/s
Collecting scipy>=0.17.0
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting joblib>=0.11
  Downloading https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl (294kB)
     |████████████████████████████████| 296kB 1.1MB/s
Collecting numpy>=1.11.0
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Installing collected packages: numpy, scipy, joblib, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.22.1 scipy-1.4.1

現在の環境にインストールされているパッケージをチェック:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

scikit-learnをインストールしただけなのにscikit-learn以外にjoblib、numpy、scipyがインストールされている。これはscikit-learnが他のパッケージjoblib、numpy、scipyに依存しているからです。つまりscikit-learn単体では動かないので、必要とされる他のパッケージを一緒にインストールしてくれたのです。

pip show コマンドでインストールしたパッケージのバージョンや依存情報を確認できます:

$ pip show scikit-learn
Name: scikit-learn
Version: 0.22.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /Users/場所/venv/lib/python3.7/site-packages
Requires: scipy, joblib, numpy
Required-by:

Requires: scipy, joblib, numpyなのでscikit-learnはscipy, joblib, numpyに依存していることが確認できます。
Required-by: が空欄になっているので、今の所scikit-learnに依存しているパッケージはないことを確認できます。

一緒にインストールされたscipyも見てみましょう:

$ pip show scipy
Name: scipy
Version: 1.4.1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author: None
Author-email: None
License: BSD
Location: /Users/場所/venv/lib/python3.7/site-packages
Requires: numpy
Required-by: scikit-learn

scipyはnumpyが必要で、scikit-learnに必要とされていることがわかります。

pip installは常に公開されている最新のバージョンをインストールするため、ある決まったバージョンのパッケージをインストールしたい時は、バージョンを明記する必要がある。
例えばscikit-learnの最新バージョンは0.22.1だが、バージョン0.21.3をインストールしたければ:

$ pip install scikit-learn==0.21.3
Collecting scikit-learn==0.21.3
  Using cached https://files.pythonhosted.org/packages/e9/57/8a9889d49d0d77905af5a7524fb2b468d2ef5fc723684f51f5ca63efed0d/scikit_learn-0.21.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Requirement already satisfied: scipy>=0.17.0 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (1.4.1)
Requirement already satisfied: joblib>=0.11 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (0.14.1)
Requirement already satisfied: numpy>=1.11.0 in ./venv/lib/python3.7/site-packages (from scikit-learn==0.21.3) (1.18.1)
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.22.1
    Uninstalling scikit-learn-0.22.1:
      Successfully uninstalled scikit-learn-0.22.1
Successfully installed scikit-learn-0.21.3

scikit-learn-0.22.1をすでにインストールしていた状態でも、まずアンインストールしてから指定したバージョンをインストールしてくれます。joblib、numpy、scipyに関しては、すでにインストールしている、かつscikit-learn-0.21.3が必要としている条件を満たしているのでそのままにしてくれます。

複数のパッケージのバージョンを指定してインストールしたい時、まとめてrequirements.txt(名前はなんでもいい)に書いてpip installできます。

$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn==0.21.3
scipy==1.4.1
$ pip install -r requirements.txt
Collecting joblib==0.14.1
  Using cached https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl
Collecting numpy==1.18.1
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting scikit-learn==0.21.3
  Using cached https://files.pythonhosted.org/packages/e9/57/8a9889d49d0d77905af5a7524fb2b468d2ef5fc723684f51f5ca63efed0d/scikit_learn-0.21.3-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting scipy==1.4.1
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Installing collected packages: joblib, numpy, scipy, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.21.3 scipy-1.4.1

現在の環境を別のプロジェクトなどに複製したい時はpip freezeコマンドで現在の環境を丸ごとファイルに書き出すことができます:

$ pip freeze > requirements.txt
$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn==0.21.3
scipy==1.4.1

'==' 以外にも'<='、 '>='などの条件をつけることができます。requirement-specifiers
例えばscikit-learn>=0.21.3という条件にした場合:

$ cat requirements.txt
joblib==0.14.1
numpy==1.18.1
scikit-learn>=0.21.3
scipy==1.4.1

現在の環境チェック:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.21.3
scipy        1.4.1
setuptools   39.0.1

pip install -r requirements.txtしてみる:

$ pip install -r requirements.txt
Requirement already satisfied: joblib==0.14.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (0.14.1)
Requirement already satisfied: numpy==1.18.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.18.1)
Requirement already satisfied: scikit-learn>=0.21.3 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 3)) (0.21.3)
Requirement already satisfied: scipy==1.4.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (1.4.1)

requirements.txtに記述されている条件を満たしていると言われる。まーscikit-learn 0.21.3はscikit-learn>=0.21.3を確かに満たしている。
条件を満たす最新のバージョンにアップデートして欲しい時には--upgrade:

$ pip install --upgrade -r requirements.txt
Requirement already up-to-date: joblib==0.14.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 1)) (0.14.1)
Requirement already up-to-date: numpy==1.18.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.18.1)
Collecting scikit-learn>=0.21.3
  Using cached https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl
Requirement already up-to-date: scipy==1.4.1 in ./venv/lib/python3.7/site-packages (from -r requirements.txt (line 4)) (1.4.1)
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.21.3
    Uninstalling scikit-learn-0.21.3:
      Successfully uninstalled scikit-learn-0.21.3
Successfully installed scikit-learn-0.22.1

また、requirementsファイルの中にrequirementsファイルを書くこともできます:

$ cat requirements_old.txt
numpy==1.18.1
scipy==1.4.1
$ cat requirements_new.txt
-r requirements_old.txt
scikit-learn>=0.21.3
$ pip install -r requirements_new.txt
Collecting numpy==1.18.1
  Using cached https://files.pythonhosted.org/packages/2f/5b/2cc2b9285e8b2ca8d2c1e4a2cbf1b12d70a2488ea78170de1909bca725f2/numpy-1.18.1-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting scipy==1.4.1
  Using cached https://files.pythonhosted.org/packages/85/7a/ae480be23b768910a9327c33517ced4623ba88dc035f9ce0206657c353a9/scipy-1.4.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting scikit-learn>=0.21.3
  Using cached https://files.pythonhosted.org/packages/82/d9/69769d4f79f3b719cc1255f9bd2b6928c72f43e6f74084e3c67db86c4d2b/scikit_learn-0.22.1-cp37-cp37m-macosx_10_6_intel.whl
Collecting joblib>=0.11
  Using cached https://files.pythonhosted.org/packages/28/5c/cf6a2b65a321c4a209efcdf64c2689efae2cb62661f8f6f4bb28547cf1bf/joblib-0.14.1-py2.py3-none-any.whl
Installing collected packages: numpy, scipy, joblib, scikit-learn
Successfully installed joblib-0.14.1 numpy-1.18.1 scikit-learn-0.22.1 scipy-1.4.1
$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

パッケージの探し方

pip searchでPyPIで公開されているパッケージを検索できます。
例えばQiitaを検索してみる:

$ pip search qiita
qiita (0.1.1)              - Qiita api wrapper for Python
qiita-spots (0.2.0)        - Qiita: Spot Patterns
qiita_v2 (0.2.1)           - Python Wrapper for Qiita API v2
qiitacli (1.1.0)           - CLI Application for Qiita API v2
qiitap (1.3.1)             - Add include function to Qiita
                             Markdown
qiita_api_wrapper (0.1.0)  - Qiita API V2 wrapper for Python
qiidly (1.0.0)             - Sync Qiita feeds for followees and
                             following tags to Feedly. -> Qiita&#
                             12391;&#12501;&#12457;&#12525;&#1254
                             0;&#20013;&#12398;&#12479;&#12464;&#
                             12392;&#12518;&#12540;&#12470;&#1254
                             0;&#12434;Feedly&#12395;&#21516;&#26
                             399;&#12290;

しかし、この方法だとパッケージの詳細がわからないので、ほとんどの場合はPyPIのウェブサイトで検索します。

パッケージをpipでアンインストール

現在の環境チェック:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
numpy        1.18.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1

パッケージをアンインストールする前にpip showでこのパッケージを必要とする他のパッケージがないことを確認してからpip uninstallしましょう。
scikit-learnはもういらないとなったら:

$ pip show scikit-learn
Name: scikit-learn
Version: 0.22.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /Users/場所/venv/lib/python3.7/site-packages
Requires: joblib, scipy, numpy
Required-by:
$ pip uninstall scikit-learn
Uninstalling scikit-learn-0.22.1:
  Would remove:
    /Users/場所/venv/lib/python3.7/site-packages/scikit_learn-0.22.1.dist-info/*
    /Users/場所/venv/lib/python3.7/site-packages/sklearn/*
Proceed (y/n)? y
  Successfully uninstalled scikit-learn-0.22.1
$ pip list
Package    Version
---------- -------
joblib     0.14.1
numpy      1.18.1
pip        19.3.1
scipy      1.4.1
setuptools 39.0.1

scikit-learnをインストールした時にはjoblib、numpy、scipyもついてきたのに、アンインストール時は残していくれるんですね!
b_1.jpg
Picture1.jpg
的な

-yで確認を飛ばせます:

$ pip uninstall joblib -y
$ pip uninstall scipy -y
$ pip uninstall numpy -y

複数アンインストール:

$ pip uninstall -y joblib scipy numpy

requirementsファイルで指定してアンインストール:

pip uninstall -r requirements.txt -y

依存関係を満たしているかチェック

pip checkコマンドでインストールされているパッケージ間の依存をチェックしてくれます。
scipyとscikit-learnはnumpyに依存するが、numpyがない場合:

$ pip list
Package      Version
------------ -------
joblib       0.14.1
pip          19.3.1
scikit-learn 0.22.1
scipy        1.4.1
setuptools   39.0.1
$ pip check
scipy 1.4.1 requires numpy, which is not installed.
scikit-learn 0.22.1 requires numpy, which is not installed.

numpyがないよと教えてくれる。

参考

What Is Pip? A Guide for New Pythonistas

2
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
13