pip list --outdatedはアップデート可能なパッケージをリストアップしてくれるが、パッケージ間の依存関係は無視される。したがって、低いバージョンを要求するパッケージによってインストールされた低いバージョンのパッケージも「アップデート可能」としてリストアップされる。さらにpip install --upgradeは依存関係を無視するので、強制的にアップデートされて、パッケージ間の依存関係が破壊される。
(sandbox) $ pip list --outdated | tail --lines='+3' | awk '{ print $1 }' | xargs --no-run-if-empty pip install --upgrade
以下は破壊された例。numpyをアップデートした結果、fasttext-langdetectのdetect関数で以下の親切なエラーメッセージが出た。ひとまずnumpy 1.x系に戻せば良いことがわかる。
ValueError: Unable to avoid copy while creating an array as requested. If using `np.array(obj, copy=False)` replace it with `np.asarray(obj)` to allow a copy when needed (no behavior change in NumPy 1.x). For more details, see https://numpy.org/devdocs/numpy_2_0_migration_guide.html#adapting-to-changes-in-the-copy-keyword.
エラーメッセージに従って、numpy 2.x未満のパッケージに差し替え。
(sandbox) $ pip install 'numpy<2'
依存関係をチェックするにはpip checkを使う。以下はmediapipe が要求する'numpy<2'の条件を無視してnumpyを2.2.3にupgradeした状態で出た依存関係の問題。
(sandbox) $ pip check
mediapipe 0.10.21 has requirement numpy<2, but you have numpy 2.2.3.
実際にエラーを起こしてみる例
numpy 2.2.3インストール済みの環境下にmediapipeをインストールすると、'numpy<2'の要件にしたがってnumpy 2.2.3から1.26.4に差し替えられる。
(sandbox) $ pip check
No broken requirements found.
(sandbox) $ pip install --upgrade numpy
Requirement already satisfied: numpy in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (1.26.4)
Collecting numpy
Using cached numpy-2.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
Using cached numpy-2.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.4 MB)
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 1.26.4
Uninstalling numpy-1.26.4:
Successfully uninstalled numpy-1.26.4
Successfully installed numpy-2.2.3
(sandbox) $ pip check
No broken requirements found.
(sandbox) $ pip install mediapipe
Collecting mediapipe
Using cached mediapipe-0.10.21-cp311-cp311-manylinux_2_28_x86_64.whl.metadata (9.7 kB)
Requirement already satisfied: absl-py in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (2.1.0)
Requirement already satisfied: attrs>=19.1.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (25.1.0)
Requirement already satisfied: flatbuffers>=2.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (25.2.10)
Requirement already satisfied: jax in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (0.5.0)
Requirement already satisfied: jaxlib in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (0.5.0)
Requirement already satisfied: matplotlib in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (3.10.0)
Collecting numpy<2 (from mediapipe)
Using cached numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)
Requirement already satisfied: opencv-contrib-python in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (4.11.0.86)
Collecting protobuf<5,>=4.25.3 (from mediapipe)
Using cached protobuf-4.25.6-cp37-abi3-manylinux2014_x86_64.whl.metadata (541 bytes)
Requirement already satisfied: sounddevice>=0.4.4 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (0.5.1)
Requirement already satisfied: sentencepiece in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from mediapipe) (0.2.0)
Requirement already satisfied: CFFI>=1.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from sounddevice>=0.4.4->mediapipe) (1.17.1)
Requirement already satisfied: ml_dtypes>=0.4.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from jax->mediapipe) (0.5.1)
Requirement already satisfied: opt_einsum in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from jax->mediapipe) (3.4.0)
Requirement already satisfied: scipy>=1.11.1 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from jax->mediapipe) (1.15.1)
Requirement already satisfied: contourpy>=1.0.1 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (1.3.1)
Requirement already satisfied: cycler>=0.10 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (4.56.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (1.4.8)
Requirement already satisfied: packaging>=20.0 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (24.2)
Requirement already satisfied: pillow>=8 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (11.1.0)
Requirement already satisfied: pyparsing>=2.3.1 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (3.2.1)
Requirement already satisfied: python-dateutil>=2.7 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from matplotlib->mediapipe) (2.9.0.post0)
Requirement already satisfied: pycparser in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from CFFI>=1.0->sounddevice>=0.4.4->mediapipe) (2.22)
Requirement already satisfied: six>=1.5 in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib->mediapipe) (1.17.0)
Using cached mediapipe-0.10.21-cp311-cp311-manylinux_2_28_x86_64.whl (35.6 MB)
Using cached numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.3 MB)
Using cached protobuf-4.25.6-cp37-abi3-manylinux2014_x86_64.whl (294 kB)
Installing collected packages: protobuf, numpy, mediapipe
Attempting uninstall: protobuf
Found existing installation: protobuf 5.29.3
Uninstalling protobuf-5.29.3:
Successfully uninstalled protobuf-5.29.3
Attempting uninstall: numpy
Found existing installation: numpy 2.2.3
Uninstalling numpy-2.2.3:
Successfully uninstalled numpy-2.2.3
Successfully installed mediapipe-0.10.21 numpy-1.26.4 protobuf-4.25.6
'numpy<2'を要求するパッケージがある状況下でもnumpyはupgradeされる。メッセージにERRORが出る。そのメッセージにしたがって、numpyをダウングレードする。
(sandbox) $ pip install --upgrade numpy
Requirement already satisfied: numpy in /path/to/venv/repository/sandbox/lib/python3.11/site-packages (1.26.4)
Collecting numpy
Using cached numpy-2.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
Using cached numpy-2.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.4 MB)
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 1.26.4
Uninstalling numpy-1.26.4:
Successfully uninstalled numpy-1.26.4
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
mediapipe 0.10.21 requires numpy<2, but you have numpy 2.2.3 which is incompatible.
Successfully installed numpy-2.2.3
(sandbox) $ pip check
mediapipe 0.10.21 has requirement numpy<2, but you have numpy 2.2.3.
(sandbox) $ pip install 'numpy<2'
Collecting numpy<2
Using cached numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (61 kB)
Using cached numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.3 MB)
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 2.2.3
Uninstalling numpy-2.2.3:
Successfully uninstalled numpy-2.2.3
Successfully installed numpy-1.26.4