コンテンツにスキップ

パッケージ管理@Python

はじめに

本サイトにつきまして、以下をご認識のほど宜しくお願いいたします。


01. PIP:Package Installer for Python

セットアップ


requirements.txtファイル

要件とするパッケージのバージョンを指定する。

flask==3.0.2
flask-bootstrap==3.3.7.1
flask-json==0.4.0

...


pip.confファイル

pipコマンドのオプションを設定する。

[global]
break-system-packages = true
timeout = 60


01-02. pipコマンド

cache

▼ purge

全てパッケージのキャッシュを削除する。

$ pip3 cache purge

▼ remove

指定したパッケージのキャッシュを削除する。

$ pip3 cache remove <パッケージ名>


check

▼ checkとは

インストールされているパッケージ間の依存関係を正しく解決できるか否かを確認する。

$ pip3 check

No broken requirements found.

解決できなかった場合は、以下のようなエラーが出力される。

$ pip3 check

wagtail 2.6.1 has requirement django-modelcluster<5.0,>=4.2, but you have django-modelcluster 5.0.


install

▼ installとは

指定したパッケージをインストールする。

$ pip3 install <パッケージ名>

▼ --upgrade

pip自身を含む、指定したパッケージをアップグレードする。

アップグレード後は、pip3 checkコマンドで依存関係が正しいかを確認し、pip3 freezeコマンドで要件ファイルも更新する必要がある。

$ pip3 install --upgrade <パッケージ名>

# 依存関係を確認する。
$ pip3 check

# 要件ファイルを更新する。
$ pip3 freeze > requirements.txt

アップグレードできるパッケージを一括でアップグレードする場合は、パイプラインと組み合わせる必要がある。

$ pip3 freeze --local \
    | grep -v '^\-e' \
    | cut -d = -f 1  \
    | xargs -n1 pip3 install -U

pip自身をアップグレードする。

$ pip3 install --upgrade pip

▼ --user

$ pip3 install --user <パッケージ名>

▼ -r

バージョン管理とrequirements.txtファイルを元にパッケージをインストールする。

$ pip3 install -r requirements.txt

指定したディレクトリにパッケージをインストールもできる。

$ pip3 install -r requirements.txt --prefix=/usr/local


freeze

▼ freezeとは

pipでインストールされたパッケージを元に、要件ファイルを作成する。

# インストールのため
$ pip3 freeze > requirements.txt

# 上書きする場合
$ pip3 freeze >| requirements.txt
# アンインストールのため
$ pip3 freeze > uninstall.txt


list

▼ listとは

現在インストールされているパッケージの一覧を取得する。

$ pip3 list

Package                    Version
-------------------------- -------
click                      8.0.3
ghp-import                 2.0.2

...

wheel                      0.37.1
zipp                       3.7.0

・-o

アップグレードできるパッケージの一覧を取得する。

$ pip3 list -o

Package            Version Latest Type
------------------ ------- ------ -----
click              8.0.3   8.0.4  wheel
importlib-metadata 4.10.0  4.11.2 wheel

...

pyparsing          3.0.6   3.0.7  wheel
setuptools         60.5.0  60.9.3 wheel


show

▼ showとは

pipでインストールしたパッケージ情報を取得する。

$ pip3 show sphinx

Name: Sphinx
Version: 3.2.1
Summary: Python documentation generator
Home-page: http://sphinx-doc.org/
Author: Georg Brandl
Author-email: georg@python.org
License: BSD
# インストール場所
Location: /usr/local/lib/python3.8/site-packages
# このパッケージの依存先
Requires: sphinxcontrib-applehelp, imagesize, docutils, sphinxcontrib-serializinghtml, snowballstemmer, sphinxcontrib-htmlhelp, sphinxcontrib-devhelp, sphinxcontrib-jsmath, setuptools, packaging, Pygments, babel, alabaster, sphinxcontrib-qthelp, requests, Jinja2
# このパッケージを依存先としているパッケージ
Required-by: sphinxcontrib.sqltable, sphinx-rtd-theme, recommonmark


uninstall

▼ uninstallとは

指定したパッケージをアンインストールする。

キャッシュが残っているので、pip3 cache removeコマンドで指定したパッケージのキャッシュも削除しておく。

$ pip3 uninstall -y <パッケージ名>

$ pip3 cache remove <パッケージ名>

uninstall.txtファイルを元にパッケージをアンインストールもできる。

キャッシュが残っているので、pip3 cache purgeコマンドでキャッシュも全て削除しておく。

$ pip3 uninstall -y -r uninstall.txt

$ pip3 cache purge


02. PIP Compile

セットアップ


requirements.inファイル

requirements.txtファイルの元になるファイル。

記法はrequirements.txtファイルと同じである。

flask==3.0.2
flask-bootstrap==3.3.7.1
flask-json==0.4.0

...

pip-compileコマンドを実行すると、requirements.txtファイルを作成できる。

#
# This file is autogenerated by pip-compile with Python 3.13
# by the following command:
#
#    pip-compile --generate-hashes requirements.in
#
flask==3.0.2 \
    --hash=sha256:3232e0e9c850d781933cf0207523d1ece087eb8d87b23777ae38456e2fbe7c6e \
    --hash=sha256:822c03f4b799204250a7ee84b1eddc40665395333973dfb9deebfe425fefcb7d
    # via
    #   -r requirements.in
    #   flask-bootstrap
    #   flask-json
flask-bootstrap==3.3.7.1 \
    --hash=sha256:cb08ed940183f6343a64e465e83b3a3f13c53e1baabb8d72b5da4545ef123ac8
    # via -r requirements.in
flask-json==0.4.0 \
    --hash=sha256:07945d66024f3b77694ce1db5d1fe83940f2aa3bcad8a608535686be67e4bc48 \
    --hash=sha256:1c1b87a657daa2179fc19f1ffc78204a716c7c5139673dc5038772db4d9f1988
    # via -r requirements.in

...


02-02. pip-compileコマンド

▼ --generate-hashes

$ pip-compile --generate-hashes requirements.in

▼ --dry-run

$ pip-compile --generate-hashes requirements.in --dry-run