dshimizu/blog

アルファ版

Debian 12 で pip でパッケージをインストールしようとしたらエラーが出るようになった

はじめに

Debian を 12 にアップグレードしたことで、 pip を使おうと思ったら以下のエラーが出るようになりました。

% pip3 install --upgrade pip
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.

メッセージを見ると、システムにインストールする場合は python3-*** の形式のパッケージをインストールし、そうでない場合は venv か pipx を使うように、といったようなものに思われました。 pipx については私は使ったことがなくわかっていません。

詳細は PEP668を見るように、とあります。

PEP 668

下記にありますが、これまでにあった OS パッケージ マネージャーと pip などの Python 固有のパッケージ管理ツールとの間の競合の問題を解決するもののようです。 ユーザー管理のパッケージはグローバルな環境ではなく、Python仮想環境にインストールするように促していく、といったもののように思われます。

Abstract

A long-standing practical problem for Python users has been conflicts between OS package managers and Python-specific package management tools like pip. These conflicts include both Python-level API incompatibilities and conflicts over file ownership.

Historically, Python-specific package management tools have defaulted to installing packages into an implicit global context. With the standardization and popularity of virtual environments, a better solution for most (but not all) use cases is to use Python-specific package management tools only within a virtual environment.

This PEP proposes a mechanism for a Python installation to communicate to tools like pip that its global package installation context is managed by some means external to Python, such as an OS package manager. It specifies that Python-specific package management tools should neither install nor remove packages into the interpreter’s global context, by default, and should instead guide the end user towards using a virtual environment.

It also standardizes an interpretation of the sysconfig schemes so that, if a Python-specific package manager is about to install a package in an interpreter-wide context, it can do so in a manner that will avoid conflicting with the external package manager and reduces the risk of breaking software shipped by the external package manager.

解決方法

内容はわかりました。

pipx のことはよくわかっていないので、今回はとりあえずPython仮想環境を使うようにしてみます。

Python仮想環境作成

Python仮想環境を作成しようとしたら下記のエラーが出ました。

% python3 -m venv ~/.local/venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.11-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: /home/dshimizu/.local/venv/bin/python3

python3.11-venv をインストールする必要があるようなのでインストールします。

% sudo apt install python3.11-venv

仮想環境を作成します。

% python3 -m venv ~/.local/venv

仮想環境を有効化します。

% cd ~/.local/venv

% source bin/activate
(venv) %

パスが通り、使えるようになりました。

(venv) % which python3
/home/dshimizu/.local/venv/bin/python3

pip でパッケージをインストールできるようになりました。

% python3 -m pip install pynvim
Collecting pynvim
  Using cached pynvim-0.4.3.tar.gz (56 kB)
  Preparing metadata (setup.py) ... done
Collecting greenlet
  Downloading greenlet-2.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (618 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 618.8/618.8 kB 15.3 MB/s eta 0:00:00
Collecting msgpack>=0.5.0
  Downloading msgpack-1.0.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (325 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 325.1/325.1 kB 44.9 MB/s eta 0:00:00
Installing collected packages: msgpack, greenlet, pynvim
  DEPRECATION: pynvim is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for pynvim ... done
Successfully installed greenlet-2.0.2 msgpack-1.0.5 pynvim-0.4.3

まとめ

Debian 12 で pip でグローバルな環境にパッケージをインストールしようとするとエラーが出るようになったため、その原因と解消方法について書きました。

参考