以下の内容はhttps://dshimizu.hatenablog.com/entry/2024/10/31/151758より取得しました。


ちょっと Python3 / pip を使いたいだけの自分向け PEP 668 の対応メモ

はじめに

pip でパッケージをインストールしようとしたら下記のようなエラーが出力されました。

% pip3 install xxx
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

PEP 668 で、pip コマンドで、Python仮想環境以外でのインストールが禁止されるルールができたようで、これによるブロックされたようです。

pip バージョン 23 からこれが適用されているようです。

Pythonistaではありませんがちょっと使いたい程度の自分に向けた調査メモです。

PEP

PEP は Python 新機能や手続き、環境を説明する設計ドキュメントとのことです。

PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment. The PEP should provide a concise technical specification of the feature and a rationale for the feature.

We intend PEPs to be the primary mechanisms for proposing major new features, for collecting community input on an issue, and for documenting the design decisions that have gone into Python. The PEP author is responsible for building consensus within the community and documenting dissenting opinions.

Because the PEPs are maintained as text files in a versioned repository, their revision history is the historical record of the feature proposal. This historical record is available by the normal git commands for retrieving older revisions, and can also be browsed on GitHub.

どうするか?

PEP 668 に則って仮想環境を使うか、無視してグローバルインストールをするかになると思います。

PEP 668 に従う場合は以下のような対応が取り得るものと思います。

venv を使う

例えば pynvim などのライブラリ類をインストールする場合、PEP 668 に則るなら仮想環境を使う形になりそうです。

適当な作業ディレクトリを作成し、activate する形になると思います。

以下ではカレントディレクトリ内に .venv というディレクトリを作って、activate してます。

% python3 -m venv ./.venv

% source venv/bin/activate

ライブラリをインストールします。

(venv) % pip3 install numpy

pipxを使う

例えば awscli などの単一アプリケーションをインストールしようと思うと pipx が使えます。

あまり意識してなかったですが、 pip でインストールできるものにはライブラリ類とコマンドなどの単一アプリケーション類で大きく2種類に別れるようです。

ライブラリ類は pipx ではインストールできません。pipを使えと言われます。

% pipx install pynvim

No apps associated with package pynvim or its dependencies. If you are attempting to install a library, pipx should not be used. Consider using pip or a similar tool instead.

pipx インストール

pipx は多くのディストリビューションのパッケージ管理システムで提供されているようで、そこからインストールできます。

% sudo apt update

% sudo apt install pipx
% pipx --version
1.1.0

この場合、バージョンが少々古いので最新を使いたい場合はソースビルドするしかなさそうです。

また、pipx で pipx をインストールするのは非推奨なようです

Warning

It is not recommended to install pipx via pipx. If you'd like to do this anyway, take a look at the pipx-in-pipx project and read about the limitations there.

終わりに

とりあえずざっくり以下のような

  • pip バージョン23 から OS へのグローバルインストールは基本的に制限される
  • PEP 668 に準拠するならライブラリのインストールには venv を使う
  • PEP 668 に準拠するなら CLIなどのスタンドアロンなアプリケーションインストールには venv か pipx を使う

グローバル環境にインストールする場合は以下のような対応になるかと思います。

  • pip コマンド実行時に --break-system-packages を用いてグローバルにインストールする
  • OS が提供しているパッケージを使う (ex. Debian なら apt install python3-pynvim など)

参考




以上の内容はhttps://dshimizu.hatenablog.com/entry/2024/10/31/151758より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14