Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pip error pkg_resources.DistributionNotFound

I'm trying to install a new module, but I found that pip doesn't work - it shows

pkg_resources.DistributionNotFound: The 'pip==1.5.6' distribution was not found and is required by the application

for each call I performed. Also, for pip --help. I found some advises to reinstall pip using pip or easy_install, but easy_install doesn't works too:

pkg_resources.DistributionNotFound: The 'setuptools==5.7' distribution was not found and is required by the application

Is there a way to fix this issue without reinstalling python and all its modules?
I have Linux Ubuntu 14.04.
apt-get install python-pip suggests installing pip (and a heap of another following software) like I don't have it in my system. I afraid to do it, would be a conflict here?

Upd.
apt-get says that it want to install the following: build-essential dpkg-dev fakeroot g++ g++-4.8 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libfakeroot libstdc++-4.8-dev python-chardet-whl python-colorama python-colorama-whl python-distlib python-distlib-whl python-html5lib python-html5lib-whl python-pip python-pip-whl python-requests-whl python-setuptools python-setuptools-whl python-six-whl python-urllib3-whl python-wheel. But as I know pip worked for me without these packages.
Also I performed
which -a python: /usr/bin/python
which -a pip: /usr/local/bin/pip
python --version: Python 2.7.6

like image 249
Jury Avatar asked Aug 14 '26 02:08

Jury


1 Answers

It would be useful to have the full backtrace of the error. One scenario that can lead to this issue, is having multiple versions of Python installed.

For example, on my Ubuntu 20.04.1 LTS I have installed Python 3.6 from source, in addition to Python 3.8 distributed with Ubuntu 20. This is what I get with pip3 --version after installing Python 3.6:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 581, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 898, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 789, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 20.2.3 (/usr/local/lib/python3.6/site-packages), Requirement.parse('pip==20.0.2'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/pip3", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3126, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3110, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3139, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 583, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 596, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 784, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==20.0.2' distribution was not found and is required by the application

The last line is the error in question, but notice an important detail in the traceback: raise VersionConflict.

This can be solved by explicitly using the required version of pip:

$ python3.6 -m pip --version
pip 20.2.3 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

Or simply:

$ pip3.6 --version
pip 20.2.3 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

Another issue one may stumble upon, is the missing lsb_release issue when doing pip3.6 install:

  File "/usr/local/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned non-zero exit status 1.

This can be solved by locating lsb_release.py and creating a symlink, for example:

sudo ln -s /usr/lib/python3/dist-packages/lsb_release.py /usr/local/lib/python3.6/site-packages/lsb_release.py

Now it's possible to upgrade pip3.6 and install new packages:

$ pip3.6 install --upgrade pip
$ pip3.6 install mypy

There are different methods for managing Python versions. Beware of using something like sudo update-alternatives --config python. Changing the system's default Python, will likely cause problems, including symptoms like the terminal not opening. It's good to have a backup terminal available, such as the one in Visual Studio Code.

like image 171
Nagev Avatar answered Aug 16 '26 21:08

Nagev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!