24

I want to install Scipy (already have Numpy installed). I have Python 3.5.1-3 installed with OS and IDLE3 (3.5.2). When I hit in terminal

sudo pip3 install scipy

It prints out

Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ImportError: cannot import name 'main'

I've already tried to reinstall pip3 and restart OS, but it didn't change. Has pip3 been working weirdly with someone else?

11 Answers11

32

Use python -m pip install instead of pip install

Example:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

I started getting this problem after a pip upgrade:

pip install --upgrade --user pip

The pip (resp. pip3) executable is provided by your distro (python-pip package on Ubuntu 16.04).

Therefore, it is not kept up-to date with the pip package itself as you upgrade pip, and may break.

If you just use python -m pip directly, e.g. as in:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

it goes through your Python path and finds the latest version of pip, and executes that file.

It relies on the fact that that file is executable, but that is a very standard type of interface, and therefore less likely to break than the hackier Debian script.

Then I recommend adding the following functions to your .bashrc:

pip() ( python -m pip "$@" )
pip3() ( python3 -m pip "$@" )

The Ubuntu 18.04 /usr/bin/pip3 file does:

from pip import main

and presumably main was removed from pip at some point which is what broke things.

The breaking pip commit appears to be: 95bcf8c5f6394298035a7332c441868f3b0169f4 "Move all internal APIs to pip._internal" which went into pip 18.0.

Tested in Ubuntu 16.04 after an update from pip3 9.0.1 to 18.0.

pyenv

Ultimately however, for serious Python development I would just recommend that you install your own local Python with pyenv + virtualenv, which would also get around this Ubuntu bug: How do I install a different Python version using apt-get?

  • 1
    So this works, but I can't be the only one who's shaking my head here. Where is the breakdown? Why does the default pip upgrade fail to set the new pip up in a way that has it continue to work? – Steven Lu Sep 18 '19 at 21:59
  • @StevenLu my understanding is that /usr/bin/pip is provided by an Ubuntu Python package, and pip does not overwrite it. But the pip update is managed by pip itself, and avoids touching Ubuntu files. – Ciro Santilli OurBigBook.com Sep 19 '19 at 06:11
  • Hi ho same experience here. Pip3 would NOT work not matter what I did. This solution works, but how come?? The python pip script is prepared by Debian --The comment says: 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. Perhaps there is a way to install Python properly in a python-way. – will Dec 10 '19 at 11:25
  • @will I'm not sure about that documentation comment, but I'm sure what the code of /usr/bin/pip does :-) – Ciro Santilli OurBigBook.com Dec 10 '19 at 13:23
  • Just to be clear, the lines you're recommending be added to .bashrc are functions, not aliases. An alias would be like alias pip3='python3 -m pip' which would also work. Using a function is fine, but has a few key differences. For example, if you exit in a function sourced by your shell, your shell session will be terminated. – vastlysuperiorman Jan 15 '20 at 16:50
  • @vastlysuperiorman thanks, I understand, updated to be more correct – Ciro Santilli OurBigBook.com Jan 15 '20 at 17:14
20

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())
Avram
  • 103
Herman
  • 201
11

numpy and scipy are in the default repositories of all currently supported versions of Ubuntu. To install numpy and scipy for Python 3.x open the terminal and type:

sudo apt update    
sudo apt install python3-numpy python3-scipy  

For Python 2.x it's:

sudo apt update  
sudo apt install --no-install-recommends python2.7-minimal python2.7 # this line is only necessary for Ubuntu 17.10 and later 
sudo apt install python-numpy # 20.04 and earlier
sudo apt install python-scipy # 18.04 and earlier
karel
  • 114,770
  • 1
    python3-scipy worked just fine. Thanks! – CSS Monteiro Apr 17 '18 at 14:04
  • 4
    As another answer states, this answer is not related to the underlying issue, which has to do with pip. – cjauvin Apr 20 '18 at 15:16
  • It's bad method... – darkwoolf Dec 10 '19 at 12:06
  • 1
    Chaining the user to pip instead using the default repositories is not always automatically the best solution. There is a security advantage to installing software from the default Ubuntu repositories. Anyone can upload software to PyPI where pip often gets its packages from, and the packages in PyPI are not reviewed as strictly as the packages in the default Ubuntu repositories. – karel Jan 17 '20 at 00:51
9

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
  • 191
6

While karel may have solved your "install numpy and scipy" problem, what's wrong with pip on your system hasn't been addressed, so you'll probably have more problems with pip going forward.

Looking here, it seems to be a pretty common recent issue with pip 10 on Ubuntu systems. You may find some work arounds on that thread that work for you, but hopefully an update will fix it soon.

JMAA
  • 307
  • Thank you for actually fixing the problem and not giving a work-around that only solves the problem partially and for only a single person... – Caleb Fenton Aug 15 '18 at 21:44
3

Installing pip from both apt and pip itself can cause this.

In my case, I used Ubuntu's pip package to install pipenv which then installed a newer copy of pip. Now because my shell runs Ubuntu's pip 9 script (to verify run which pip3) and my Python interpreter then imports the pip 10 module, the pip3 command fails. So I want to uninstall one of the two.

It's fair to assume you have the newer pip for a reason. In that case you want to uninstall the older pip like so:

sudo apt remove python3-pip

If you know for sure that you're fine with the older pip and prefer the system package you'll want to uninstall the newer one:

~/.local/bin/pip3 uninstall pip

or failing that

sudo /usr/local/bin/pip3 uninstall pip

Jeff C
  • 31
1

My issue ended up being a mismatch between python3.6 and 3.7. The python3.6 installation put a link in /usr/bin/python3 -> /usr/bin/python3.6 even though the system had upgraded to python3.7.

sudo apt purge python-pip
sudo apt purge python3-pip
sudo apt install python3.7 --reinstall
cd /usr/bin
sudo rm python3
sudo ln -s python3.7 python3
python3 --version
pip3 --version
1

type

hash -d pip3

This should remove this error

Bob
  • 111
1

Force reinstalling pip works just fine for most of the users as shown on this github page:

python -m pip install --force-reinstall pip
-1

This worked for me:

pip install --upgrade --user pip

By install --upgrade, I mean whatever you're trying to install.

Ghasem
  • 119
  • 5
-1

I ran into this problem after a fresh mint install. The following worked for me:

sudo apt install python3-distutils

Solution found here:

luQ
  • 99