190

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.

Armend Veseli
  • 143
  • 2
  • 9
Grimdrem
  • 2,001
  • 2
  • 9
  • 4
  • Make sure that `/usr/local/lib/pythonX.X/site-packages` precedes `/usr/lib/pythonX.X/site-packages` in your `$PATH`. – hoefling Apr 20 '18 at 12:19
  • Same issue here. Did you find a solution? – WayBehind Apr 20 '18 at 16:49
  • 6
    @WayBehind I did found a solution. I noticed that using `python -m pip` everything works just fine. Beside that, I removed the file `/usr/local/bin/pip`, now calling `pip -V` return `pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)`. Everything seems to work as expected. – Grimdrem Apr 20 '18 at 18:15
  • I still have the same problem, I am using pip in conda environment , python 2.7 and I am using pip install ipdb. Any idea about this? – Garvita Tiwari Sep 07 '18 at 08:37
  • 1
    Followed the steps plus the -H flag: `sudo -H python3.7 get-pip.py --force-reinstall` – Andre Leon Rangel May 26 '19 at 23:50

32 Answers32

220

This did it for me:

python -m pip install --upgrade pip

Environment: OSX && Python installed via brew

nbro
  • 15,395
  • 32
  • 113
  • 196
magicrebirth
  • 4,104
  • 2
  • 25
  • 22
  • 8
    Thank you, this was the solution for me using python3. – Furbeenator Aug 31 '18 at 16:50
  • 2
    this did not work for me. what @Grimdrem did is what I ended up doing. I had(in the past) installed pip for all (using sudo or something)...and my upgrade was with --user so the other install still hung around...removing it from /usr/local/bin and from my main python's package folder (not --user's) did the trick. – user1269942 Oct 29 '18 at 20:58
  • Strangely this has fixed it here, too. I was to upgrade #searx to latest commit. – Roland Dec 01 '19 at 14:33
  • Ran into this on Ubuntu on WSL. I also use `pyenv`, but had somehow muffed the system python which `vim` uses. This put things where they needed to be.and now `taskwiki` works again. Whew! – Daniel Black Apr 09 '20 at 18:04
  • This worked on ubutnu as well – kittu Nov 11 '21 at 16:44
  • If your system is old enough to have python2 still present, you'll need to `python3 -m pip install --upgrade pip` instead. – Dale C. Anderson Feb 12 '22 at 01:02
  • This gives me: `/usr/bin/python3.6: No module named pip.__main__; 'pip' is a package and cannot be directly executed`. – einpoklum Aug 23 '23 at 17:15
88

An answer from askUbuntu works.

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

Problem solved. Also works for python3.

Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
chris
  • 2,761
  • 17
  • 24
  • 9
    This isn't working for me, I've tried this a few times – Cleve Green May 02 '18 at 14:08
  • 1
    I've fixed it by reinstalling python, which was an absolute pain now that I have none of my past modules – Cleve Green May 22 '18 at 15:32
  • Just bitten by this issue, seems to be a bit of a cluster that `apt-get install python-pip` doesn't work on 16.04.4 and you to resort to binary blob installers? – Joe Jun 01 '18 at 02:27
  • 1
    to make it work I had to remove all my *system side* pip things: `apt-get remove python-pip-whl` – Jan Aug 17 '18 at 03:28
  • "Permission denied". You probably need to use sudo. Most distros don't allow vanilla users to install pip system wide. – Cerin Mar 04 '19 at 19:00
69

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

vauhochzett
  • 2,732
  • 2
  • 17
  • 40
Abdallah Okasha
  • 1,879
  • 17
  • 20
38

Are you using python 2 or python 3? 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.
22

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

zangw
  • 43,869
  • 19
  • 177
  • 214
  • 1
    That's old Python2. On Python3 Ubuntu at least you should also run `sudo easy_install-3.4 pip` or similar. – NoBugs Feb 04 '19 at 04:38
  • This fixed the issue for me when I have to upgrade pip 20.2.3 to pip 21.0 on MacOS. Thank you! – Alex L Sep 22 '20 at 14:40
19

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.

Yi Yang Apollo
  • 321
  • 1
  • 4
17

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

GauthamGAjith
  • 367
  • 1
  • 4
  • 19
13

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

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
  • I got the error `no such option: --uprade` on your command - so I ran `python3 -m pip install pip` and it worked :) – xeruf May 22 '20 at 10:34
7

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.

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
  • 2
    This solved it for me, thanks. I simply saved $PYTHONPATH somewhere, then ran `export PYTHONPATH=""` to get pip working. – ibowman May 10 '18 at 18:34
  • but when trying echo $PYTHONPATH, site-packages wasn't included. – alyssaeliyah May 26 '18 at 16:34
  • I encountered this when building a virtual environment on a system that uses modules to manage software packages. When I loaded you say "module load python/3.5.2" and it prepends your `PYTHONPATH`. Then when I activated my virtual env, it never unset my PYTHONPATH, which meant it found the system level `pip` and not the one in my virtualenv. – irritable_phd_syndrome Jun 22 '18 at 19:08
7

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
subtleseeker
  • 4,415
  • 5
  • 29
  • 41
abbas khan
  • 233
  • 2
  • 4
  • 7
  • Update the answer to last version of pip ``curl https://bootstrap.pypa.io/pip/3.5/get-pip.py -o get-pip.py`` And install it ``py get-pip.py --force-reinstall`` – Eng_Farghly Jun 06 '22 at 08:30
5

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.

tospo
  • 646
  • 9
  • 17
  • As mentioned in this answer https://stackoverflow.com/a/50737296/1340995 it's a permission issue. I found that using pip with `python3 -m pip` solves this issue as well, probably because `python` has permission to execute and it doesn't need pip to have the +x permission as it's just loading the file and not executing it. – Maxime Feb 26 '20 at 20:09
5

For me

python -m pip uninstall pip

solved the issue. Reference

hariszaman
  • 8,202
  • 2
  • 40
  • 59
  • 1
    worked on MacOS, thanks! (don't reinstall, turns out it was a conflicting installation) – Soid May 22 '21 at 15:26
3

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

sudo apt remove python pip
Tinashe Makuti
  • 131
  • 1
  • 3
  • For what it's worth, this appears to be the solution for a Debian-like distro (_e.g._, Ubuntu 16.04) where the `python-pip` package was installed at the distro level, and `get-pip.py` had been used for a user-specific version of `pip` which was also in the `$PATH`. I `sudo apt-get remove`d the `python-pip` package (2.7) and used the one in my ~/.local/bin instead. No more error message. – TheDudeAbides Dec 20 '18 at 21:34
3

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

Ateeb
  • 5,413
  • 3
  • 10
  • 17
2

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

GeorgesD
  • 49
  • 3
  • My case was weirder because "sudo" still gave me the same error. I had to switch to root (sudo su on ubuntu) to be able to do pip3.5 list and pip3.5 install :| – jmng Jul 24 '18 at 18:00
2

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.

jdhao
  • 24,001
  • 18
  • 134
  • 273
2

Checking from pip documentation, this command worked to me:

python -m ensurepip --upgrade
  • 1
    I am building python from source inside Docker. Then I am adding a virtual environment in that python version. pip command does not work. After all attempts I did from over and there, only this has worked for me (when running it in that virtual environment). THANKS! – babis21 Apr 13 '23 at 19:56
2

pip is not being installed properly on your "user", so try the following :

sudo python3 -m pip install --user --upgrade pip
1

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"

Merwanski
  • 29
  • 3
  • 1
    This one works for me however you have to change it to the python version you are using. sudo python3 get-pip.py --force-reinstall. Great. – alyssaeliyah May 26 '18 at 16:36
1

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

Mihai.Mehe
  • 448
  • 8
  • 13
0

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

bayocr
  • 1
  • 2
0

(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

Gil Hiram
  • 492
  • 5
  • 13
0

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
John
  • 111
  • 1
  • 4
0

This command works for me.

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

HTL
  • 11
  • 2
0

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

yum remove python-pip
yum install python-pip
0

I fixed this problem by

sudo apt-get install python3-pip

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

Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
0

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.

N.Hung
  • 154
  • 6
0

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.

0

windows OS:

1、download this file:“https://bootstrap.pypa.io/get-pip.py”,Put it in this(E:\PythonProject\venv\Scripts’(Your virtual environment installation directory)) directory!
2、open ‘Windows PowerShell’
3、cd ‘E:\PythonProject\venv\Scripts’(Your virtual environment installation directory)
4、run cmd ‘py get-pip.py’
CG Zhang
  • 186
  • 1
  • 6
0

Please verify the directory permission for /usr/local/lib/python3.9/ and modify the permission using chown command

sudo chown -R centos:centos /usr/local/lib/python3.9/

its helps me.

0

In some cases, this can occur because pip was upgraded by the root user, and the upgrade - for some reason - defaults to creating non-all-user-accessible subdirectories and files in the "site-packages" directory.

To check if this is the case, we need locate the site-packages directory, e.g. with python3, running this command:

python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])'

Now let's check whether there are pip-related subdirs there with problematic permissions:

site_pkg_dirs=$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
pushd ${site_pkg_dirs}
ls -ld pip*
popd

If the output is empty - then this answer won't solve your problem; sorry :-(

But if the output is not empty, e.g.:

drwxr-x--- 5 root root 111 Jan 01 00:00 pip
drwxr-x--- 2 root root 170 Jan 01 00:00 pip-21.3.1.dist-info

then carefully examine the permission listing on each: If the "all" permission triplet doesn't have r and x - you need to add those; and since this same problem might be manifested for other packages, let's be more thorough about it:

find ${site_pkg_dirs} -type d | chmod a+rx
find ${site_pkg_dirs} -type f | chmod a+r

this ensures all subdirectories in your site package directory are readable and recursable, and that all files in those subdirectories are readable. Specifically, this will apply to the pip-related subdirs, which should now look like this when you ls -l them:

drwxr-xr-x 5 root root 111 Jan 01 00:00 pip
drwxr-xr-x 2 root root 170 Jan 01 00:00 pip-21.3.1.dist-info

This should solve your pip troubles! Let's check: If you now run

pip show pip

you should get something like :

$ pip3.6 show pip
Name: pip
Version: 21.3.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: distutils-sig@python.org
License: MIT
Location: /usr/lib/python/site-packages
Requires:
Required-by:

possibly with a different version and your own site packages dir.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

I also had this problem because some of my folders got corrupted due to a hard disk failure, due to this the pip_internal folder also got corrupted. To fix this, I first ran a disk repair restart so that the corrupted folders get removed from the drive, after that I deleted the whole pip folder from the site packages (Python>Lib>site-packages>pip).

Then I ran this command in terminal: python -m ensurepip and it reinstalled the whole pip package. After this pip is working fine.

Akascape
  • 219
  • 2
  • 11