2
2

【備忘録】Ubuntu 24.04 で Python3.12 の Pip を利用する

Last updated at Posted at 2024-08-06

導入

Pythonの仕様の変更とOSのバージョンにより,これまでの pip3 install xxx のように Ubuntu で pip コマンドが使えなくなった.これによって,基本的に Python は venv の仮想環境下でのみの実行しか行えなくなっている.例えば,numpy をインストールしようと,以下のようにコマンドを実行するとエラーが出力される.

$ pip3 install numpy
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.12/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.

ちなみに,get-pip.py https://pip.pypa.io/en/stable/installation/#get-pip-py を使った方法を試しても同様のメッセージが出力される.

方法

参考程度にPEP668を軽く眺めてみたところ,実質,仮想環境でしか Python を動かせなくなったと言って良いのかもしれない.後述するが,pipでのパッケージのインストールは仮想環境 venv でしか行えなくなり,さらにインストールしたパッケージは仮想環境上でしか動作しないのである.

以下に示す対処方法は基本的に下に挙げた参考記事のものと同様である.

  1. sudo apt install python3-venv を実行し,仮想環境のための準備をする
  2. python3 -m venv 仮想環境のパス で作成.例えば,ホームディレクトリ上にvenv_pyという名前の仮想環境を作成する場合は python3 -m venv ~/venv_py となる
  3. source ~/仮想環境のパス/bin/activate で仮想環境をアクティベートする.上記の例では,source ~/venv_py/bin/activate となる
  4. すると,Ubuntuのターミナルでは $ マークの前に仮想環境の名前がカッコ付きで表れる.これが,仮想環境をアクティベートしている状態である
  5. あとはいつもどおり,pip3 install xxxの形式でほしいパッケージをインストールすれば良い.ただし,導入でも前述したように,pipでインストールしたパッケージを用いたプログラムを実行する場合は仮想環境を立ち上げて置かなければならないことに注意する

私の理解不足でこれまで通り,仮想環境など気にせず python を実行できる方法はあるかもしれない.しかし,PEP668を見る限り,そもそもそのようなプログラムの実行はあまり推奨されていないのであろう.

ちょっとした tips

以上のように,仮想環境上で python を動かすことが前提になった以上,いちいち terminal を立ち上げ,python3 を使うたびに source ~/venv_py/bin/activate などと打つのは面倒だろう.かと言って,~/.bashrc に書くのも,python を利用しないときまで,仮想環境が立ち上がるのはなんだか気持ちが悪い.

そこで私がおすすめしたいのが Tmux である.Tmuxであれば,tmux-resurrect を使うことで,ターミナルのセッションを閉じても,さらに言えば,シャットダウンしても残る(参考:https://zenn.dev/nbtk/articles/df7f64e3550b07 )ので,python 用のセッションを tmux で作成しておけばよいだろう.

追記(20240808)

https://blog.jp.square-enix.com/iteng-blog/posts/00043-play-with-the-pep668/ には,仮想環境なしで強制的に pip3 でパッケージをインストールする方法が紹介されていました.

参考にした記事

【私用】ubuntu24.04でpipしてもライブラリインストールできずPEP668になる件
"This environment is externally managed"のエラーが出たとき、python仮想環境をつかう。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2