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  
2
  • You said 16.04 in your post but used the 14.04 tag. Which version is correct?
    – edwinksl
    Oct 26, 2017 at 11:21
  • 1
    16.04 is correct. I couldn't find the 16.04 tag just now. Sorry.
    – mlpy
    Oct 26, 2017 at 11:28

8 Answers 8

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
4
  • 4
    I tried but still the same error: imgur.com/a/nAdqU .
    – mlpy
    Oct 26, 2017 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. Jan 16, 2018 at 8:07
  • @Kristada673 Try reading the answers to this question: askubuntu.com/questions/363200/…. DragonLord's answer worked for me.
    – karel
    Jan 16, 2018 at 8: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. Jan 14, 2021 at 6: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)

1
  • 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, 2019 at 4: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.
4
  • While that maybe fixes things, can you please expand your answer ? Why to remove this directory (maybe add a caveat about the python3.5 path), why never to run it with sudo, and so on.. Jan 22, 2018 at 15:17
  • I am not quite an expert on this - I just wanted to share what had worked for me. Jan 29, 2018 at 23:19
  • After hours of searching SO and SE, this is the only thing that fixed my issue
    – Old Markus
    Feb 11, 2021 at 16:16
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
0

Had same issue, installing openssl fixed it for me

1
  • But openssl already install on my Ubuntu: imgur.com/a/60kIM . Maybe it's caused by another reason.
    – mlpy
    Oct 26, 2017 at 12:56
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.

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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .