[python] pip: no module named _internal

I have a problem when I try to use pip in any way. I'm using Ubuntu 16.04.4

I should say that I've used it already, and I never had any problem, but starting today when I use any command I always get the same error (as an example using pip --upgrade).

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

I have tried doing sudo apt-get remove python-pip followed by sudo apt-get install python-pip but nothing changed.

This question is related to python pip

The answer is


Nothing worked for me, but only one thing: I used sudo in front of the command and it is working fine.


you can remove it first, and install again ,it will be ok. for centos:

yum remove python-pip
yum install python-pip

(On windows) not sure why this was happening but I had my PYTHONPATH setup to point to c:\python27 where python was installed. in combination with virtualenv this produced the mentioned bug.

resolved by removing the PYTHONPATH env var all together


For completeness, I just encountered this problem with "Ubuntu latest" ... v18.04 ... and fixed it in this way:

python3 -m pip install --upgrade pip

(Notice that it was necessary to specify python3 since this references Python 3.6.9. The python command on the same system references Python 2.7.17. Since this is apparently a system-wide installation it encountered a ["not sudo" ...] permission error, but it didn't matter because it was the wrong thing to do anyway. I was encountering the problem with pip3.)


The following solution solved the problem on my machine for python2.7 "$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py" and then "$ sudo python2.7 get-pip.py --force-reinstall"


I have fixed this error by running the following commands:

sudo apt remove python-pip
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py

It will remove the previously installed pip and reinstall it. Thanks :)


These often comes from using pip to "update" system installed pip, and/or having multiple pip installs under user. My solution was to clean out the multiple installed pips under user, reinstall pip repo, then "pip install --user pip" as above.

See: https://github.com/pypa/pip/issues/5599 for an official complete discussion and fixes for the problem.


This command works for me.

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


my solution: first step like most other answer:

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

second, add soft link

sudo ln -s /usr/local/bin/pip /usr/bin/pip

I met the same error on Windows when I tried to install a package via pip3:

Traceback (most recent call last):
  File "d:\anaconda\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\anaconda\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Anaconda\Scripts\pip3.6.exe\__main__.py", line 5, in <module>
ModuleNotFoundError: No module named 'pip._internal'

My python is installed via Anaconda. I solved this issue by reinstalling pip via conda:

conda install pip

After that, pip returns to normal.


An answer from askUbuntu works.

For pip2.7, you can at first curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py, then python2.7 get-pip.py --force-reinstall to reinstall pip.

Problem solved. Also works for python3.


In file "/usr/local/bin/pip" change from pip._internal import main to from pip import main


I've seen this issue when PYTHONPATH was set to include the built-in site-packages directory. Since Python looks there automatically it is unnecessary and can be removed.


Refer to this issue list

sudo easy_install pip

works for me under Mac OS

For python3, may try sudo easy_install-3.x pip depends on the python 3.x version. Or python3 -m pip install --user --upgrade pip


For the current user only:

easy_install --user pip

or

python -m pip install --upgrade --user pip

The second may give /usr/bin/python: No module named pip Even if which pip finds the module named pip. In this case try the easy_install


This solution works for me:

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

or use sudo for elevated permissions (sudo python3 get-pip.py --force-reinstall).

Of course, you can also use python instead of python3 ;)

Source


I have the same problem on my virtual environment after upgrade python installation from 3.6 to 3.7 but only on vent globally pip work fine, to solve it I deactivate and delete my virtual environment after recreate again and now is fine, on venv:

deactivate
rm -rvf venv 

and after recreate the virtual environment. I use mac OS 10.11, and python 3


Its probably due to a version conflict, try to run this, it will remove the older pip somehow.

sudo apt remove python pip

I just encountered the same problem and in my case, it turns out this is a conflict between the python installation in my virtualenv and the site-wide python (Ubuntu). What solves it for me is to run pip in this way, to force usage of the correct python installation (in my vortualenv):

python3 -m pip install PACKAGE

instead of

pip3 install PACKAGE

I realised this when I tried to follow some of the answers here that suggest re-installing pip and the error output I got was pointing to an existing site-wide python library path although I had activated my virtualenv. Worth trying before deleting and re-installing stuff.


I fixed this problem by

sudo apt-get install python3-pip

this worked even for python2.7, amazing...


For me

python -m pip uninstall pip

solved the issue. Reference


My solution is adding import pip to the script linked to the pip/pip3 commands.

Firstly, open the file (e.g. /usr/local/bin/pip) with your favorite text editor and the sudo mode. For example, I use sudo vim /usr/local/bin/pip to open the script file.

You will obtain some file as following:

import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Afterwards, insert the statement import pip just before the from pip._internal import main then the issue is resolved.


Are you using python2 or python3? The following commands could be different!

  1. run python3 -m pip --version to see if you have pip installed.
  2. if yes, run python3 -m pip install --upgrade pip.
  3. if no, run sudo apt-get install python3-pip, and do it again.

This did it for me:

python -m pip install --upgrade pip

Environment: OSX && Python installed via brew


I tried the following command to solve the issue and it worked for me:

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

This issue maybe due to common user do not have privilege to access packages py file.
1. root user can run 'pip list'
2. other common user cannot run 'pip list'

[~]$ pip list
Traceback (most recent call last):
  File "/usr/bin/pip", line 7, in <module>
from pip._internal import main
ImportError: No module named pip._internal

Check pip py file privilege.

[root@]# ll /usr/lib/python2.7/site-packages/pip/  
?? 24  
-rw-------  1 root root   24  6?  7 16:57 __init__.py  
-rw-------  1 root root  163  6?  7 16:57 __init__.pyc  
-rw-------  1 root root  629  6?  7 16:57 __main__.py  
-rw-------  1 root root  510  6?  7 16:57 __main__.pyc  
drwx------  8 root root 4096  6?  7 16:57 _internal  
drwx------ 18 root root 4096  6?  7 16:57 _vendor  

solution : root user login and run

chmod -R 755 /usr/lib/python2.7 

fix this issue.