ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

【Raspberry Pi】pip install が失敗する

Raspberry Pi で「pip install」を実行したところ以下のようなエラーになってしまいました。

test@raspberrypi:~/Desktop/samples $ pip install -r requirements.txt
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.
    
    For more information visit http://rptl.io/venv

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.

原因

一般ユーザー権限で、Debian でパッケージ化されていない Python アプリケーションパッケージを pip を使ってインストールしようとするとエラーになるそうです。

回避策

簡単な方法

インストール時にコマンドオプションに「--break-system-packages」を付ければ回避できるとありました。

$ pip install --break-system-packages {{パッケージ名}}

pipx を使ったインストール

$ sudo apt-get install pipx
test@raspberrypi:~/Desktop/samples $ pipx ensurepath
Success! Added /home/test/.local/bin to the PATH environment variable.

Consider adding shell completions for pipx. Run 'pipx completions' for instructions.

You will need to open a new terminal or re-login for the PATH changes to take effect.

Otherwise pipx is ready to go! ✨ 🌟 ✨
追加された設定を有効化
$ source ~/.bashrc