11
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pipのVersionConflictを修正して、ライブラリインストール実行を正常に行う

Last updated at Posted at 2020-05-04

image.png

「なんでPythonのパッケージ管理すぐ死んでしまうん?」

環境

  • 今回起きた環境は以下、すでにPipfileは作成済
$ python -V
Python 3.7.6

起きたこと

  • pip installとかpipenv installあたりのライブラリインストールがうまく動かなくなった
  • pipのバージョンみても以下のようにエラーが吐かれていました
$ pip -V
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 583, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 791, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 20.1 (/usr/local/lib/python3.7/site-packages), Requirement.parse('pip==19.3.1'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/opt/python/libexec/bin/pip", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3251, in <module>
    @_call_aside
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3235, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3264, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 585, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 598, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==19.3.1' distribution was not found and is required by the application

原因

  • これっぽい、pipのバージョンがコンフリクトを起こしているみたい
  • 要求されているpipは19.3.1で、実行しているpipは20.1だから
    • どこかでupgradeした説(Warningでるからね...)
pkg_resources.VersionConflict: (pip 20.1 (/usr/local/lib/python3.7/site-packages), Requirement.parse('pip==19.3.1'))

対策

  • 要求されているpipのバージョン(今回は19.3.1)をインストールする
  • インストール方法はPyPAの公式ドキュメントに記載されていました
  • また、ローカル環境のPythonとpipがひもづいていない場合にも、この方法は有用です
# python get-pip.pyを実行するためのファイルをcurlで取得
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1824k  100 1824k    0     0  1650k      0  0:00:01  0:00:01 --:--:-- 1650k

# 引数でpipのバージョンを指定してpipをインストールする
$ python get-pip.py pip==19.3.1
Looking in indexes: https://pypi.python.org/simple/
Collecting pip==19.3.1
  Downloading pip-19.3.1-py2.py3-none-any.whl (1.4 MB)
     |████████████████████████████████| 1.4 MB 553 kB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.1
    Uninstalling pip-20.1:
      Successfully uninstalled pip-20.1
Successfully installed pip-19.3.1

結果

  • 指定されたバージョンのpipがローカルのPythonと紐付いてダウンロードされています
  • あとは好きにpip installなりpipenv installなりしてください
$ pip -V                                                                                                                                       
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
11
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
11
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?