ウェブサイト検索

Debian 11にPIP Pythonパッケージマネージャをインストールして使用する方法


Debian 11にPIP Pythonパッケージマネージャをインストールして使用する方法

このページで

  1. 前提条件
  2. Python3用のPipをインストールする
  3. Python2用のPipをインストールする
  4. Pipコマンドラインの使い方
  5. 結論

PipはPythonプログラミング言語の広く使われているパッケージマネージャーです。これは、Python標準ライブラリにはない追加のパッケージをインストールおよび管理するために使用されます。ユーザーはPythonパッケージインデックスからパッケージを検索し、その依存関係をインストールすることができます。Pipは「Preferred Installer Program(推奨インストーラープログラム)」としても知られており、Pythonアプリケーションのための完全に孤立した環境を作成することができます。

この記事では、Debian 11にPipをインストールし使用する方法を紹介します。

前提条件

  • Debian 11を実行しているサーバー。
  • サーバーにはルートパスワードが設定されています。

Python3用のPipをインストールする

デフォルトでは、PipはDebian 11オペレーティングシステムにインストールされていません。Python3とPython2のために別々のPipバージョンをインストールする必要があります。

まず、次のコマンドを使用してPython3をインストールしてください:

apt-get install python3 -y

Python3パッケージがインストールされたら、次のコマンドを使ってPython3用のPipをインストールしてください。

apt-get install python3-pip -y

次に、以下のコマンドを使用してPipのバージョンを確認してください。

pip3 --version

次の出力が得られます:

pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9)

Python2用のPipをインストールする

まず、システムにPython2をインストールする必要があります。次のコマンドを使用してインストールできます:

apt-get install python2 curl -y

次に、以下のコマンドを使用してPip2インストールスクリプトをダウンロードします:

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

次に、ダウンロードしたスクリプトを実行して、Pip2をシステムにインストールします。

python2 get-pip.py

インストールが完了したら、以下のコマンドを使用してPip2のバージョンを確認できます:

pip2 --version

次の出力が得られます:

pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

Pipコマンドラインの使い方

Pipで利用可能なすべてのオプションをリストするには、次のコマンドを実行してください:

pip3 --help

次のリストを受け取ります:

Usage:   
  pip3  [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.
  cache                       Inspect and manage pip's wheel cache.
  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.

Python3のためにTextStatisticのようなパッケージをインストールするには、次のコマンドを実行します:

pip3 install "TextStatistic"

サンプル出力:

Collecting TextStatistic
  Downloading TextStatistic-1.0.6-py3-none-any.whl (5.6 kB)
Installing collected packages: TextStatistic
Successfully installed TextStatistic-1.0.6

Python2のパッケージ(例えばscrapy)をインストールするには、以下のコマンドを実行します:

pip install "scrapy"

利用可能なパッケージをすべて一覧表示するには、次のコマンドを実行します:

pip3 list

次の出力が表示されるはずです:

Package          Version
---------------- ---------
certifi          2020.6.20
chardet          4.0.0
httplib2         0.18.1
idna             2.10
pip              20.3.4
pycurl           7.43.0.6
PySimpleSOAP     1.16.2
python-apt       2.2.1
python-debian    0.1.39
python-debianbts 3.1.0
reportbug        7.10.3
requests         2.25.1
setuptools       52.0.0
six              1.16.0
TextStatistic    1.0.6
urllib3          1.26.5
wheel            0.34.2

パッケージを検索するには、次のコマンドを実行してください:

pip3 search urllib3

古いパッケージをリストするには、以下のコマンドを実行してください:

pip3 list --outdated

次の出力が得られます:

Package          Version   Latest    Type
---------------- --------- --------- -----
certifi          2020.6.20 2021.10.8 wheel
httplib2         0.18.1    0.20.1    wheel
idna             2.10      3.3       wheel
pip              20.3.4    21.3      wheel
pycurl           7.43.0.6  7.44.1    sdist
python-debian    0.1.39    0.1.40    wheel
python-debianbts 3.1.0     3.2.0     wheel
requests         2.25.1    2.26.0    wheel
setuptools       52.0.0    58.2.0    wheel
urllib3          1.26.5    1.26.7    wheel
wheel            0.34.2    0.37.0    wheel

任意のパッケージの情報を表示するには、次のコマンドを実行します:

pip3 show wheel

次の出力にホイールパッケージの情報が表示されるはずです:

Name: wheel
Version: 0.34.2
Summary: A built-package format for Python
Home-page: https://github.com/pypa/wheel
Author: Daniel Holth
Author-email: [email 
License: MIT
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by: 

任意のパッケージをアンインストールするには、以下のコマンドを実行してください:

pip3 uninstall scrapy

結論

上記のガイドでは、Debian 11にPip3およびPip2をインストールする方法を説明しました。また、Pythonパッケージをインストールおよび管理するためにPipコマンドの使い方も説明しました。これで、Pipコマンドを使ってPythonの依存関係を簡単に管理できるようになることを願っています。

関連記事: