213

I installed Python 3.x (besides Python 2.x on Ubuntu) and slowly started to pair modules I use in Python 2.x.

So I wonder, what approach should I take to make my life easy by using pip for both Python 2.x and Python 3.x?

Taku
  • 28,570
  • 11
  • 65
  • 77
theta
  • 22,873
  • 36
  • 114
  • 154
  • 3
    Personally, I'd avoid global package installs and use virtualenv. Then you use pip from inside your virtual environment for each project, and you're always using the right one. – jpmc26 Aug 24 '13 at 06:38

10 Answers10

200

The approach you should take is to install pip for Python 3.2.

You do this in the following way:

$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python3.2 get-pip.py

Then, you can install things for Python 3.2 with pip-3.2, and install things for Python 2-7 with pip-2.7. The pip command will end up pointing to one of these, but I'm not sure which, so you will have to check.

rAntonioH
  • 129
  • 2
  • 12
Lennart Regebro
  • 158,668
  • 41
  • 218
  • 248
  • 9
    This was easy, thanks. `sudo python3.2 get-pip.py` installs `pip` and `pip-3.2` scripts in `/usr/local/bin` and both logically use Python 3. `sudo python get-pip.py` installs `pip` and `pip-2.7` here, so in this case `pip` uses Python 2.7. I additional created link to `pip-3.2` as `pip3` and tested: Perfect! :) – theta Jun 30 '12 at 08:51
  • 6
    On Fedora, it's pip3.3, that is, without the dash between pip and the version. Check `/usr/bin` to see which pip versions you have there. – Shailen Jul 31 '14 at 22:24
  • 5
    \[x\]Ubuntu is also using `[ pip2 | pip2.7 | pip3 | pip3.4 ]`. Is this a change in `pip` or different between different systems? – Jon Surrell Feb 18 '15 at 11:12
  • After executing I got `You're using an outdated location for the get-pip.py script, please use the one available from https://bootstrap.pypa.io/get-pip.py` – Kevin Brotcke Feb 01 '16 at 04:39
  • So do that then. :-) – Lennart Regebro Feb 03 '16 at 11:44
  • FYI, Mac UNIX also uses [ pip2 | pip2.7 | pip3 | pip3.4 ] – Lucas Spangher May 31 '18 at 23:26
  • Manjaro / Arch Linux also uses `pip#.#` (i.e. without the hyphen). – James Ray Oct 29 '18 at 06:27
  • This is likely rephrasing previous comments, but if "pip" doesn't work try "pip3". For me the former points to pip2.7, which (I assume) caused the OP issue. – Max Aug 23 '19 at 22:49
  • On WSL I get ModuleNotFoundError: No module named 'apt_pkg' – Shell_Leko Oct 14 '20 at 16:58
59

What you can also do is to use apt-get:

apt-get install python3-pip

In my experience this works pretty fluent too, plus you get all the benefits from apt-get.

Erik Pragt
  • 12,458
  • 10
  • 52
  • 58
  • 2
    On 12.04 I can't do this. – dranxo Aug 05 '14 at 17:49
  • 16
    I successfully used this (ie sudo apt-get install python3-pip) and then could install python3 packages using "sudo python3 -m pip install package". – Tom Slee Oct 24 '14 at 13:34
  • 2
    As of today, I believe apt-get gets you the outdated 1.5.6 version; if you don't want an AssertionErrror during `pip freeze > requirements` (or other potential bugs), do install from source for the latest version and save yourself some headache. – Yibo Yang Oct 03 '15 at 06:29
  • must be root to install `python3-pip` – Florent Apr 25 '19 at 09:06
  • Despite of installing `pip` using `python3.7` `pip` is still being used default with Python 2.7 on ubuntu 14.04 :( I have managed to update pip to the latest version ie. 19.3.1 but its still taking default to python2.7. How to make it use with python 3.7? – Curious Developer Nov 26 '19 at 13:26
  • Warning for the comment above, never use `sudo` to install arbitrary python packages. See [the risks here](https://stackoverflow.com/questions/21055859/what-are-the-risks-of-running-sudo-pip) – 林果皞 Jul 28 '20 at 13:21
  • This question doesn't have a `unix` tag. What about windows? – Eduardo Pignatelli Aug 06 '20 at 11:43
52

First, install Python 3 pip using:

sudo apt-get install python3-pip

Then, to use Python 3 pip use:

pip3 install <module-name>

For Python 2 pip use:

pip install <module-name>
masquerade817
  • 183
  • 9
  • 9
25

The shortest way:

python3 -m pip install package
python -m pip install package
fiveelements
  • 3,343
  • 1
  • 16
  • 16
  • ``` (test-exif) alex@alex-xps-13-9370:~/projects/oe/app/exif$ python3 -m pip install exif /usr/bin/python3: No module named pip ``` – axd Aug 19 '19 at 13:52
  • @axd in that case first install pip using `sudo apt-get install python3-pip`. Assuming you are using python3. – fiveelements Aug 19 '19 at 14:45
15

If you don't want to have to specify the version every time you use pip:

Install pip:

$ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python3

and export the path:

$ export PATH=/Library/Frameworks/Python.framework/Versions/<version number>/bin:$PATH
tldr
  • 11,707
  • 14
  • 71
  • 117
  • 11
    Could you elaborate? I don't see how installing *distribute* has anything to do with not having to specify version of *pip* you want to use. – Piotr Dobrogost Nov 08 '13 at 18:53
6

In Windows, first installed Python 3.7 and then Python 2.7. Then, use command prompt:

pip install python2-module-name

pip3 install python3-module-name

That's all

Community
  • 1
  • 1
Kardi Teknomo
  • 1,139
  • 13
  • 21
5

This worked for me on OS X: (I say this because sometimes is a pain that mac has "its own" version of every open source tool, and you cannot remove it because "its improvements" make it unique for other apple stuff to work, and if you remove it things start falling appart)

I followed the steps provided by @Lennart Regebro to get pip for python 3, nevertheless pip for python 2 was still first on the path, so... what I did is to create a symbolic link to python 3 inside /usr/bin (in deed I did the same to have my 2 pythons running in peace):

ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/pip /usr/bin/pip3

Notice that I added a 3 at the end, so basically what you have to do is to use pip3 instead of just pip.

The post is old but I hope this helps someone someday. this should theoretically work for any LINUX system.

Ordiel
  • 2,289
  • 3
  • 35
  • 51
4

On Suse Linux 13.2, pip calls python3, but pip2 is available to use the older python version.

karsten
  • 603
  • 4
  • 11
4
  1. To use pip for a Python 2.x environment, use this command:

    py -2 -m pip install -r requirements.txt
    
  2. To use pip for Python 3.x environment, use this command:

    py -3 -m pip install -r requirements.txt
    
TylerH
  • 20,816
  • 57
  • 73
  • 92
Anurag Daware
  • 401
  • 1
  • 5
  • 15
3

Please note that on msys2 I've found these commands to be helpful:

$ pacman -S python3-pip
$ pip3 install --upgrade pip
$ pip3 install --user package_name
user8128167
  • 6,133
  • 6
  • 59
  • 71