149

I've installed the latest python (2.7.9) bundled with pip and setuptools for windows 32-bit. I've tried reinstalling pip but the problem persists.

Here's the error after running pip --version in Administrator cmd:

Traceback (most recent call last):
 File "D:\Python\lib\runpy.py", line 162, in _run_module_as_main
  "__main__", fname, loader, pkg_name)
 File "D:\Python\lib\runpy.py", line 72, in _run_code 
  exec code in run_globals
 File "D:\Python\Scripts\pip.exe\__main__.py", line 5, in <module>
ImportError: cannot import name main
Kobi K
  • 7,743
  • 6
  • 42
  • 86
Woootiness
  • 1,882
  • 2
  • 15
  • 18
  • 1
    It may be related to file permission there was an issue with python 3.4 [here](http://bugs.python.org/issue21030) that was solved, it worth a check. – Kobi K Jan 29 '15 at 08:48
  • I did the workarounds using the icacls command but the error persists. – Woootiness Jan 30 '15 at 09:24
  • 2
    "easy_install -U pip" answer provide at http://stackoverflow.com/questions/28031277/pip-install-error-cannot-import-name- unpack-url – J1MF0X Apr 02 '16 at 11:42

16 Answers16

254

The bug is found in pip 10.0.0.

In linux you need to modify file: /usr/bin/pip from:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

to this:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())
dma1324
  • 231
  • 1
  • 7
catalinpopescu
  • 2,573
  • 1
  • 7
  • 3
  • 1
    It seems this is the solution. I tried this and no problem so far. – f10w Apr 19 '18 at 11:37
  • 2
    I am using pip 10.0.1 and the bug is still there. You solution helped. Thanks! Have you reported this bug and the solution back to the developers? – Alexey Subach Apr 24 '18 at 15:10
  • 7
    Although this solution works, please refrain from modifying `pip` itself. See a cleaner solution below to call `pip` from Python instead. – Wan B. Apr 26 '18 at 06:14
  • 1
    Doesn't help on "pip 10.0.1". `pip --version pip 10.0.1 from /home/x/.local/lib/python2.7/site-packages/pip (python 2.7)` usage: `sudo pip install tensorflow` output: `Traceback (most recent call last): File "/usr/bin/pip", line 9, in from pip import main ImportError: cannot import name main` – littleAlien Apr 27 '18 at 14:09
  • (Using pip 10.0.1) The bug seems to only affect normal users, so the bugfix works but brakes the superuser activities... kind of weird – FedFranz Jun 11 '18 at 15:02
  • @WanBachtiar Any reason to refrain from modifying pip itself? – vishalmullur Aug 09 '18 at 11:12
  • 3
    @creepy_driver , manually modifying an installed package to suit the environment is not recommended because next time you update to another version, i.e. 10.0.3 you would likely to encounter the same issue. The workaround of running `pip` via installed Python will point pip to use the suitable environment correctly. – Wan B. Aug 10 '18 at 01:54
  • In my case, `pip3 -V` gives 18.0, while the `python3-pip` debian package version is at `9.0.1-2`. But yes, this was a solution. – Sergiy Kolodyazhnyy Sep 03 '18 at 23:00
  • **Do not modify pip if you are using pipenv!!!**. I was using nginx, flask and uwsgi. But somehow, uwsgi failed to identify the correct virtualenv path given to it. Even though the absolute path given was correct. I restored pip to it's original state and all was fine thereafter. The problem was on **Ubuntu 16.04** though. – python_noob Oct 31 '18 at 03:16
  • This bug seems to persist even in pip 18.1 (tested: python 2.7). I just had to apply this patch on pip on fresh installed Debian Jessie 9.6. – xZero Nov 13 '18 at 15:36
  • it resolved my problem with pip 18.1, Python 3.6.7, Ubuntu 18.10 – xuemind Dec 20 '18 at 13:12
  • 1
    This bug still exists in pip 18, I wonder why no one hasn't fixed this problem – gameon67 Feb 28 '19 at 05:54
  • AttributeError: 'module' object has no attribute 'main' – lxknvlk Jun 06 '19 at 07:59
  • I do really agree with Wan Bachtiar – Alex8752 Aug 26 '19 at 01:43
176

Even though the original question seems to be from 2015, this 'bug' seems to affect users installing pip-10.0.0 as well.

The workaround is not to modify pip, however to change the way pip is called. Instead of calling /usr/bin/pip call pip via Python itself. For example, instead of the below:

pip install <package>

If from Python version 2 (or default Python binary is called python) do :

python -m pip install <package>

or if from Python version 3:

python3 -m pip install <package> 
Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • yeah, I don't remember what I did to fix this but I'm keeping this open for others. – Woootiness May 03 '18 at 10:04
  • When I utilized the given command, I got a permissions error (that I also got after altering pip according to catalinpopescu's answer). However, to download packages for the user's scope (so that you do not need administration rights) use: python3 -m pip install --user (works for python3, should also work for python2 although not tested) – randmin Oct 15 '18 at 00:38
  • Helped me on a Mac. – charles ross Nov 09 '18 at 21:27
  • Better answer. Modifying the source code is a swamp. – ozgur Dec 29 '18 at 19:48
  • This is a really good way to go. It also makes it really easy to get assurance you're using the right version: `python2 -m pip install $package` – JamesTheAwesomeDude Nov 20 '19 at 17:57
58

On Ubuntu Server 16, I have the same problem with python27. Try this:

Change

from pip import main
if __name__ == '__main__':
    sys.exit(main())

To

from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())
DeadChex
  • 4,379
  • 1
  • 27
  • 34
Shuai Liu
  • 688
  • 6
  • 10
22

On Windows 10, I used the following commands to downgrade pip:

python -m pip uninstall pip
python -m pip install pip==9.0.3

This should also work on Linux and Mac too.

kiyah
  • 1,502
  • 2
  • 18
  • 27
  • 1
    It should be python -m pip install pip==9.0.3 – elelias Jun 01 '18 at 09:55
  • @elelias Oh, I didn't realize that. thanks for telling me! – kiyah Jun 01 '18 at 13:07
  • 2
    I had to `python easy_install.py pip==9.0.3` because after the first line, pip wasn't found. – Suzanne Jun 15 '18 at 20:11
  • 3
    Works on Linux and Mac too. It's a work-around until pip/pip3 10 is fixed. Folks should not waste their time editing installed files not intended to be modified after installation.. – Richard Elkins Jun 22 '18 at 16:49
  • I had path issues after the upgrade, i did the following to make it work. sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall – webjockey Apr 16 '19 at 23:18
  • this worked. I had to add --user in the end due to permission issue. – pbou Jul 19 '19 at 12:51
10

I had the same problem, but uninstall and reinstall with apt and pip didn't work for me.

I saw another solution that presents a easy way to recover pip3 path:

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
Hamza Ali
  • 380
  • 5
  • 21
  • 1
    I had python 2.7, so i had to alter the command to match python 2.7 sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall – webjockey Apr 16 '19 at 23:17
  • works perfectly on ubuntu but I think it won't work on windows – zaki benz Oct 11 '19 at 08:35
8

i fixed the problem by reinstalling pip using get-pip.py.

  1. Download get-pip from official link: https://pip.pypa.io/en/stable/installing/#upgrading-pip
  2. run it using commande: python get-pip.py.

And pip is fixed and work perfectly.

Bachir Mehemmel
  • 159
  • 3
  • 3
2

On MacOS if you've installed python via Homebrew, change the line in /usr/local/opt/python/libexec/bin/pip

from

from pip.internal import main

to

from pip._internal import main

Or use this one liner: sed -i '' "s/from pip import main/from pip._internal import main/" /usr/local/opt/python/libexec/bin/pip

Explanation:

The issue is caused by the changes in pip version 10 moving internal namespace under main._internal and the bin script put in place by homebrew still looking it from the old place (where it used to be in version 9). Issue and some discussion https://github.com/pypa/pip/issues/5240

Madis Nõmme
  • 1,264
  • 2
  • 15
  • 25
1

If you have a hardlink to pip in your PATH (i.e. if you have multiple python versions installed) and then you upgrade pip, you may also encounter this error.

The solution consists in creating the hardlink again. Or even better, stop using hardlinks and use softlinks.

Martín De la Fuente
  • 6,155
  • 4
  • 27
  • 28
1

On Windows 10, I had the same problem. PIP 19 was already installed in my system but wasn't showing up. The error was No Module Found.

python -m pip uninstall pip
python -m pip install pip==9.0.3

Downgrading pip to 9.0.3 worked fine for me.

Debu Shinobi
  • 2,057
  • 18
  • 21
0

For those having similar trouble using pip 10 with PyCharm, download the latest version here

0

try this

#!/usr/bin/python
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.i
try:
    from pip import main
except ImportError:
    from pip._internal import main
if __name__ == '__main__':
    sys.exit(main())
Abdul Gaffar
  • 340
  • 3
  • 5
0

A simple solution that works with Ubuntu, but may fix the problem on windows too:

Just call

pip install --upgrade pip
juergi
  • 1,333
  • 1
  • 9
  • 16
0

This solved my problem in ubuntu 18.04 when trying to use python3.6:

rm -rf ~/.local/lib/python3.6

You can move the folder to another place using mv instead of deleting it too, for testing:

mv ~/.local/lib/python3.6 ./python3.6_old
RDlady
  • 378
  • 2
  • 16
0

Open your terminal linux.

hash -d pip
  • Hi Carlos, welcome to Stack Overflow. To aid other users facing this problem, please could you add to you answer to clarify what this command does and how it fixes the issue. – Foxocube Nov 22 '19 at 18:20
0

In our case, in 2020 using Python3, the solution to this problem was to move the Python installation to the cloud-init startup script which instantiated the VM.

We had been encountering this same error when we had been trying to install Python using scripts that were called by users later in the VM's life cycle, but moving the same Python installation code to the cloud-init script eliminated this problem.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
-1

It works on ubuntu 16.04. Step 1:

 sudo gedit /home/user_name/.local/bin/pip

a file opens with the content:

#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

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

Change the main to __main__ as it appears below:

#!/usr/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip import __main__

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

Save the file and close it. And you are done!