17

I installed python3 and pip3 successfully on my Ubuntu16.04, but pip3 install is broken. How can I fix this problem? The error information of pip3 install is as follows:

# pip3 install xlwt
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
  from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 21, in <module>
  from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
ModuleNotFoundError: No module named 'pip._vendor.requests'

Output of sudo -H pip3 install --upgrade pip

sudo -H pip3 install --upgrade pip  
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
  from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 21, in <module>
  from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
ModuleNotFoundError: No module named 'pip._vendor.requests'

Output of which pip3 and pip3 --version:

# which pip3
/usr/bin/pip3

# pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
ModuleNotFoundError: No module named 'pip._vendor.requests'

P.S. Python2 pip runs successfully. Output of "which pip" and "pip --version":

# which pip
/usr/bin/pip

# pip --version
pip 1.5.4 from /usr/local/lib/python2.7/dist-packages/pip-1.5.4-py2.7.egg (python 2.7)

And python and python3 installation information:

# which python
/usr/bin/python
# which python3
/usr/bin/python3

# python -V
Python 2.7.14
# python3 -V
Python 3.6.3  
karel
  • 114,770
mlpy
  • 373

8 Answers8

16

There is something wrong with your pip3 so remove it and reinstall it. Open the terminal and type:

sudo apt purge python3-pip  
sudo rm -rf '/usr/lib/python3/dist-packages/pip'  
sudo apt install python3-pip   
cd
cd .local/lib/python3/site-packages
sudo rm -rf pip*  
cd
cd .local/lib/python3.5/site-packages
sudo rm -rf pip*  
python3 -m pip install xlwt
karel
  • 114,770
  • 4
    I tried but still the same error: https://imgur.com/a/nAdqU . – mlpy Oct 26 '17 at 15:21
  • I am having the same issue too. When I do sudo apt install python3-pip, I get the following error: The following packages have unmet dependencies: python3-pip : Depends: python-pip-whl (= 8.1.1-2) but 8.1.1-2ubuntu0.4 is to be installed Recommends: build-essential but it is not going to be installed Recommends: python3-dev (>= 3.2) but it is not going to be installed Recommends: python3-wheel but it is not going to be installed E: Unable to correct problems, you have held broken packages. – Kristada673 Jan 16 '18 at 08:07
  • @Kristada673 Try reading the answers to this question: https://askubuntu.com/questions/363200/e-unable-to-correct-problems-you-have-held-broken-packages. DragonLord's answer worked for me. – karel Jan 16 '18 at 08:10
  • @karel What's the purpose of the --user parameter on your last command line? Without explanation, it looks like something specific to your environment. – FKEinternet Jan 14 '21 at 06:26
6

Installing python3-pip package create a python script in file /usr/bin/pip3. In order to run, main() function need to be imported from module pip (from pip import main). This method path is only available for packaged pip version (9.0.1 in my case).

After running pip3 install --upgrade pip, pip version become 18.1, and main() has been moved in pip._internal.

Use the command python3 -m pip --version to see if your case correspond to the same situation (pip3 is also available with this method when /usr/bin/pip3 is broken).

If so, in file /usr/bin/pip3, replace line 9: from pip import main with: from pip._internal import main

The issue will be fixed. (Tested on Ubuntu 18.04 distribution)

slava
  • 3,887
Vincent H.
  • 61
  • 1
  • 2
  • After I changed the import, a different error shows up File "/usr/bin/pip3", line 11, in <module> sys.exit(main()) TypeError: 'module' object is not callable – Mzq Oct 30 '19 at 04:48
3

This is what I've found helpful:

  1. sudo rm -rf /usr/local/lib/python3.5/dist-packages
  2. Never run pip3 within sudo.
2

First delete the corrupted pip3:

rm /user/bin/pip3 /user/local/bin/pip3

Reinstall the new one:

sudo apt-get remove python3-pip
sudo apt-get install python3-pip
Eliah Kagan
  • 117,780
邹若奇
  • 21
  • 1
0

As @Vincent H. mentioned, i encountered the same issue.

Traceback (most recent call last): File "/home/ashish/.local/bin/pip3", line 7, in from pip._internal import main ImportError: No module named 'pip._internal'

The fix to the issue was to replace pip._internal to pip in .local/bin/pip3 by doing sudo gedit .local/bin/pip3.

TheLazy
  • 101
0

This is an problem but u can solution.

rm /user/bin/pip3 /user/local/bin/pip
sudo apt-get remove python-pip

than reinstall

sudo apt-get install python-pip
Hello
  • 1
0

u may do this:

sudo apt purge python3-pip  
sudo rm -rf '/usr/lib/python3/dist-packages/pip'  
sudo apt install python3-pip
sudo cp /usr/bin/pip3 /usr/local/bin/

This worked for me when my pip3 was broken

GN Vageesh
  • 101
  • 1
0

Had same issue, installing openssl fixed it for me

  • But openssl already install on my Ubuntu: https://imgur.com/a/60kIM . Maybe it's caused by another reason. – mlpy Oct 26 '17 at 12:56