はじめに
ある日、pip install を実行したらこんなログが出ました。
[notice] To update, run: python.exe -m pip install --upgrade pip
てことでなんも考えず言われた通り python.exe -m pip install --upgrade pip を実行。
しかし、またある日、同じパッケージ、同じバージョンを指定し pip install を実行してみると...
ERROR: Cannot install requests==2.25.0 and urllib3==2.0.0 because these package versions have conflicting dependencies.
「この前は普通にインストールできたのにエラー出た!なんでや!」
涙とまらん![]()
Q.なんでバージョン上げただけなのにエラッた?
A.パッケージの依存関係の衝突が検出されるようになったから
pip 20.3以降、インストールされるパッケージの依存関係検出が厳密化され、依存関係が壊れているパッケージ同士のインストールは未然に防がれる形で、pip install がエラー終了するようになりました。
詳しくは公式ドキュメントを参照
実際にこの現象を再現してみよう
仮想環境で実際に依存関係の壊れているパッケージ同士を pip install してみましょう。
1.仮想環境を作成、有効化
C:\Users\karaage>python -m venv testenv
C:\Users\karaage>testenv\Scripts\activate
(testenv) C:\Users\karaage>pip -V
pip 25.3 from C:\Users\karaage\testenv\Lib\site-packages\pip (python 3.14)
(testenv) C:\Users\karaage>
作成時の pip のバージョンは 25.3 ですね。
2.依存関係の壊れているバージョンのパッケージ同士を requirements.txt に記載
(testenv) C:\Users\karaage>type requirements.txt
requests==2.25.0
urllib3==2.0.0
(testenv) C:\Users\karaage>
この場合、requests 2.25.0 は urllib3 < 1.27 である必要があるので、依存関係が壊れています。
3.pip installしてみる
(testenv) C:\Users\karaage>pip install -r requirements.txt
Collecting requests==2.25.0 (from -r requirements.txt (line 1))
Downloading requests-2.25.0-py2.py3-none-any.whl.metadata (4.2 kB)
Collecting urllib3==2.0.0 (from -r requirements.txt (line 2))
Downloading urllib3-2.0.0-py3-none-any.whl.metadata (6.6 kB)
Collecting chardet<4,>=3.0.2 (from requests==2.25.0->-r requirements.txt (line 1))
Downloading chardet-3.0.4-py2.py3-none-any.whl.metadata (3.2 kB)
Collecting idna<3,>=2.5 (from requests==2.25.0->-r requirements.txt (line 1))
Downloading idna-2.10-py2.py3-none-any.whl.metadata (9.1 kB)
INFO: pip is looking at multiple versions of requests to determine which version is compatible with other requirements. This could take a while.
ERROR: Cannot install -r requirements.txt (line 1) and urllib3==2.0.0 because these package versions have conflicting dependencies.
The conflict is caused by:
The user requested urllib3==2.0.0
requests 2.25.0 depends on urllib3<1.27 and >=1.21.1
Additionally, some packages in these conflicts have no matching distributions available for your environment:
urllib3
To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip to attempt to solve the dependency conflict
[notice] A new release of pip is available: 25.3 -> 26.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts
(testenv) C:\Users\karaage>
お、期待通りエラー終了しましたね。
てか、25.3すら最新じゃなかったですね
4.pip のバージョンを 20.2 以下に下げる
(testenv) C:\Users\karaage>C:\Users\karaage\testenv\Scripts\python.exe -m pip install pip==20.2
Collecting pip==20.2
Using cached pip-20.2-py2.py3-none-any.whl.metadata (3.7 kB)
Using cached pip-20.2-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 25.3
Uninstalling pip-25.3:
Successfully uninstalled pip-25.3
Successfully installed pip-20.2
[notice] A new release of pip is available: 20.2 -> 26.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
(testenv) C:\Users\karaage>
これで pip のバージョンは 20.2 になりましたね。
5.もう一回 pip installしてみる
pip 20.2 で pip install する場合、Python 3.8〜3.10 である必要があります。
(testenv) C:\Users\karaage>pip install -r requirements.txt
Collecting requests==2.25.0
Downloading requests-2.25.0-py2.py3-none-any.whl (61 kB)
Collecting urllib3==2.0.0
Downloading urllib3-2.0.0-py3-none-any.whl (123 kB)
Collecting chardet<4,>=3.0.2 (from requests==2.25.0->-r requirements.txt (line 1))
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting idna<3,>=2.5 (from requests==2.25.0->-r requirements.txt (line 1))
Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting certifi>=2017.4.17 (from requests==2.25.0->-r requirements.txt (line 1))
Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
Installing collected packages: urllib3, chardet, idna, certifi, requests
Successfully installed certifi-2023.7.22 chardet-3.0.4 idna-2.10 requests-2.25.0 urllib3-2.0.0
(testenv) C:\Users\karaage>
おお、インストールできた!
この通り、pip 20.2 では壊れた依存関係のパッケージ同士もインストールできてしまいました。
まとめ
pip のバージョンを上げたことによって出るエラーというのは、一見 pip が悪さをしているように感じてしまいますが、実際はインストールしようとしているパッケージの依存関係が良くなかったということですね。
これと近い話で、パッケージのバージョン更新 pip install --upgrade <パッケージ名> も、
迂闊に実行すると依存関係が崩れる恐れがあるので、
--dry-run での事前確認や、pip freeze > requirements.txt でのバックアップ作成など対策をし、慎重にバージョン更新を行うようにしましょう。