6

I upgraded to Ubuntu 16.04. I know it came with only python 3 installed, but somehow python 2 got in there ( I guess as a dependency). That's ok because i would add it anyway. Both python versions are present as can be seen with:

$ python --version
Python 2.7.11+
$ python3 --version
Python 3.5.1+

My problem is that pip and pip3 both default to python3 (and there's no pip2 either). For example:

$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python3.5/dist-packages
$ sudo -H pip3 install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python3.5/dist-packages

For what it's worth they are referring to different fodlers:

$ which pip
/usr/local/bin/pip
$ which pip3
/usr/local/bin/pip3

How do I make pip install to python2 and pip3 install to python3 like in Ubuntu 14.04?

9
  • Ubuntu 16.04 has Python 2 and 3 in the installation. It's just that as of now, everything from Ubuntu is supposed to use Python 3 instead of 2. And anything in /usr/local is not from Ubuntu. It's installed by you, probably by compiling Python from source.
    – muru
    Jun 1, 2016 at 18:20
  • It's not compiled from source - haven't learnt how to do that. In any case what do i need to fix to add packages to python 2 as well?
    – Karsus
    Jun 1, 2016 at 18:24
  • Ty running /usr/bin/pip. Ubuntu's pip is still for Python 2.
    – muru
    Jun 1, 2016 at 18:25
  • ll | grep pip in /usr/bin shows only pip3 results for some reason !?
    – Karsus
    Jun 1, 2016 at 18:40
  • Then install it: sudo apt-get install python-pip.
    – muru
    Jun 1, 2016 at 18:40

4 Answers 4

7

after installing both

sudo apt-get install python-pip
sudo apt-get install python3-pip

under Ubuntu 16.04 I can simply use

pip2 install module
pip3 install module

to install the desired module for Python2 or Python3, respectively.

I hope that is helpful and works for you, too!

2

Face same issue solved by unlinking pip from python3 by reinstalling it as following:

sudo python -m pip install -U --force-reinstall pip
0

I found a way that works, and hopefully I didn't break anything in the process. At first I tried editing the first line in /usr/local/bin/pip to change it from python 3 to python 2 compiler but got error that there was no module named pip !?

So I thought maybe I need install it for python 2. So I went there and saved the get-pip.py script they offered. There is a warning that it might break stuff for the OS but since xenial uses only python3 I decided to risk it. And it worked:

$ sudo -H pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python2.7/dist-packages
$ sudo -H pip3 install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /usr/local/lib/python3.5/dist-packages

There is still the potential problem that stuff are in /usr/local that muru mentioned but I don't know much about it and if stuff keep working I will not complain. The only ways I ve added packages are apt-get and some .deb files in rare exceptions (like chrome iirc).

1
  • 1
    I think the reason numpy is in /usr/local is that I installed it through pip and not through apt. Just mentioning it here in case it helps someone.
    – Karsus
    Jul 12, 2016 at 16:27
0

I know this is an old thread but since none of the answers fixed the issue for me and I could not find any other solution online I thought I would share what finally worked for me.

Running these two lines corrected the problem for me and now pip installs modules for python2

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

python get-pip.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .