162

Running pip or pip3 results with:

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

I had issues with this, and uninstalled pip3, but when i try to install it again using

sudo apt-get -y install python3-pip

it does install, but then running pip or pip3 i get the same error.

#which pip3
/home/myuser/.local/bin/pip3
Yaron
  • 13,173
Keweik
  • 1,721
  • 2
    I have a similar problem after upgrading from pip 9.0.2 to pip-10.0.0. Ubuntu 16.04LTS here. – Dave Apr 15 '18 at 11:19
  • 3
    You should not upgrade to pip 10 on Ubuntu, because the system version installed through apt is modified in a way not compatible to pip 10. See https://github.com/pypa/pip/issues/5221 @Dave – Byte Commander Apr 15 '18 at 12:27
  • macOS 10.13.4, same problem after upfgrade to pip 10 – Benjamin R Apr 21 '18 at 04:38
  • 3
    Temporary workaround: python3 -m pip install --user <package> – Benjamin R Apr 21 '18 at 22:14
  • 1
    if you have easy_install (comes from python-setuptools package), you can do sudo easy_install pip (or sudo easy_install3 pip for python3-only, etc.) – Marek Sebera May 25 '18 at 14:40
  • How did you install Pip originally? It can't have been done through the package manager since that wouldn't result in executables under ~/.local/bin. – David Foerster Jul 10 '18 at 16:37
  • A simple reboot of the machine may resolve the issue; if you're not going to lose any work then try this before reinstalling anything. – olisteadman Aug 03 '18 at 07:20
  • Can't answer here yet, but I had the same issue and had probably mixed apt and non-apt installs of pip. Purged the bins on my path and installed through apt, problem solved. – Frans Sep 06 '19 at 09:25
  • https://github.com/pypa/pip/issues/5373 Solution on above link: Before using virtualenv: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py --force-reinstall And I also uninstalled the latest virtualenv and install sudo pip install virtualenv==15.1.0 – Ganesh S Nov 18 '19 at 09:36
  • I had this happening in a Docker multi-stage build when I was accidentally trying to run a version of pip from one stage that was installed using Python 3 in another stage using Python 2. – esmail Oct 07 '20 at 20:10
  • See my answer (use python -m ... not python3 -m ... at https://stackoverflow.com/questions/18363022/importerror-no-module-named-pip/66956587#66956587 – Victoria Stuart Apr 05 '21 at 16:53
  • A sudo apt install --reinstall python3-pip python-pip solved this issue for me on Debian. – ndvo Jun 20 '21 at 17:33

8 Answers8

252

After upgrading pip (or pip3, in this case) if the following occurs:

$ ~ pip3 -V
Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal import main
ModuleNotFoundError: No module named 'pip._internal'

Force a reinstall of pip:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py --force-reinstall

Verify install:

$ ~ pip3 -V
pip 10.0.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

Now pip3 install <package> and pip3 install --user <package> (for user-level installs) will work correctly.

There should never, ever be any reason you need to run pip in elevated mode.

For Python 2.7

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python get-pip.py --force-reinstall

Had same problem on macOS as well, it's a common issue across platforms.

Benjamin R
  • 2,952
  • 1
  • 13
  • 20
  • 2
    Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/bin/pip3' – endolith Aug 21 '18 at 10:44
  • 1
    @endolith Okay, now you want to use sudo rm -rf /usr/bin/pip3 – purge that existing directory first, then install from scratch. It's a permissions problem on that directory, but it's better to reset in these circumstances I believe, you can always easily reinstall whatever packages you lose again. If that doesn't work, nuke your Python 3 install, too, then reboot. – Benjamin R Aug 23 '18 at 08:57
  • @BenjaminR How do I nuke all the system python stuff and reinstall it? I'm sure I've done the wrong thing many times over the years – endolith Aug 23 '18 at 13:35
  • 1
    @endolith Hi, it depends on what your OS (and distro, if Linux) is. Let me know and I’ll try to point you in the right direction. p.s. We’ve all been in your position more times than most of us like to admit :) – Benjamin R Aug 24 '18 at 14:35
  • @BenjaminR Ubuntu, just updated from last LTS to 18.04 – endolith Aug 24 '18 at 20:45
  • 1
    @endolith Okay! See: https://www.fosslinux.com/3534/how-to-completely-uninstall-applications-by-command-line-in-ubuntu.htm – Benjamin R Aug 25 '18 at 11:57
  • 1
    @endolith Once you've done that, run which python / which python3. If nothing turns up, reboot your machine, then reinstall python 2/3 using apt-get install <package name>. Finally, if you run in to something unexpected, run find / -iname python* (you'll probably need sudo permissions for these commands). If anything turns up which is a directory with a binary, remove the enclosing directory, then reboot. – Benjamin R Aug 25 '18 at 12:03
  • 1
    This also works on WSL (Windows Subsystem for Linux) on Windows 10. – Max Candocia Feb 09 '20 at 04:43
  • 1
    In my case it was due to python actually resolving to an older version of pip module installed via other means and not via OS apt-get. That older version (8.1) likely lacks the __internal module and once I uninstalled it (python -m pip uninstall pip) the 18.1 pip package installed via apt-get was getting picked up and yes sir it does have an __internal module with the correct permissions etc. – Edi Bice Jul 08 '20 at 17:23
  • None of this worked and none of this explains why OP's approach didn't work. Always explain why something doesn't work in the first place. Guessing doesn't explain. – yujaiyu Jun 10 '22 at 13:30
  • @foki You are correct, it's not an answer, only a solution. It's indeed best to provide the reason. Do you know the answer? I remember looking it up at the time but I've forgotten exactly why it was occurring. If so please tell us or edit my answer? (edited my original reply because I was misreading where you were coming from) – Benjamin R Jun 12 '22 at 05:16
  • 1
    @BenjaminR The problem occurs because the Python interpreter cannot find pip._internal module on PYTHONPATH. pip is just yet another Python module. To figure out why it cannot be found, one must look at their PYTHONPATH and the location of the Python interpreter in their particular case (especially when there are multiple Python installations). Thus, reinstalling pip the way you pointed out may or may not solve anything. In sum, to get to a solution valuable to more than one narrow case, we need to understand what we are doing. – yujaiyu Jun 12 '22 at 17:36
60

I solved this by updating pip via Python, like this:

python2 -m pip install --user --upgrade pip
python3 -m pip install --user --upgrade pip
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
cdutra
  • 1,149
27

This command also works. It reinstalls pip:

sudo easy_install pip
cdutra
  • 1,149
9

Apply these three steps:

  1. Go to /usr/local/bin by terminal
  2. Execute sudo gedit pip
  3. Change the from pip._internal import main into from pip import main.
zx485
  • 2,426
7

Check if pip is already installed using

pip3 -V 

or

pip3 --version

If not use this command to install it:

sudo apt install python3-pip

Now you can use

python3 -m pip install packageName

to install packages using pip.

Zanna
  • 70,465
3

I got the same problem as you just now, I found the reason is that you are working without superuser privilege since some internal python packages or modules are installed under superuser privilege.

So you can try by fist entering sudo su, then enter your password, and run pip install, it might help.

Daniel
  • 141
  • 4
    You should never, ever need to run pip with elevated permissions. Use --user flag instead, as in: pip3 install --user <package> – Benjamin R Apr 21 '18 at 04:44
  • 1
    However, when I run without superuser permission, I still got the information like
    pip3 install --user Traceback (most recent call last): File "/usr/local/bin/pip3", line 7, in \ from pip._internal import main ImportError: No module named 'pip._internal' Could you please help me to figure out why this always happen?
    – Daniel Apr 22 '18 at 05:21
  • 2
    Ok, I found out why, if from pip._internal import main error happen, in my solution, 1) curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 2) python3 get-pip.py --user --force-reinstall, and then you can run pip without superuser permission, Thank you very much for your advice, it helps a lot ! – Daniel Apr 22 '18 at 05:36
  • My pleasure! Look, I learned the hard way to be careful about using sudo willy-nilly (destroyed my OS multiple times!) – Benjamin R Apr 23 '18 at 07:34
  • Hmmm https://askubuntu.com/a/802594/5032 – endolith Aug 21 '18 at 10:40
2

A force re-install of pip with -H flag worked for me:

sudo -H python3.7 get-pip.py --force-reinstall
Eliah Kagan
  • 117,780
zoozoo
  • 151
  • Do not use sudo to install pip: https://askubuntu.com/questions/802544/is-sudo-pip-install-still-a-broken-practice – Benjamin R Apr 17 '23 at 05:48
1

The pip version now is 19.0.1:

which pip3
#/home/xxx/.local/bin/pip3
vim /home/xxx/.local/bin/pip3

Change from pip._internal import main into from pip import main

Alan Lau
  • 11
  • 1
  • 1
    Traceback (most recent call last): File "/usr/local/bin/pip3", line 5, in from pip import main ImportError: cannot import name 'main' from 'pip' (/usr/lib/python3/dist-packages/pip/init.py) – fccoelho Jan 10 '20 at 16:17