217

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
  • 31,927
  • 11
  • 74
  • 85
theta
  • 24,593
  • 37
  • 119
  • 159
  • 4
    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

11 Answers11

205

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
  • 167,292
  • 41
  • 224
  • 251
  • 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
  • In 2023, this is unfortunately wrong for many platforms. You probably already have `pip`, you just need to make sure the one on your `PATH` is the right version. – tripleee Jan 12 '23 at 06:57
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
  • 13,513
  • 11
  • 58
  • 64
  • 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
55

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>
gaurang847
  • 323
  • 6
  • 11
30

The shortest way:

python3 -m pip install package
python -m pip install package
fiveelements
  • 3,649
  • 1
  • 17
  • 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
  • This assumes that you are on a platform which uses `python3` for Python 3 and `python` for Python 2. This used to be a fairly common convention on many Linux distributions, but as Python 2 is falling out of use, some are reverting back to using `python` for Python 3. – tripleee Jan 12 '23 at 06:22
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,924
  • 15
  • 75
  • 120
7

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,375
  • 16
  • 24
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,442
  • 3
  • 36
  • 52
4

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

karsten
  • 639
  • 5
  • 13
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,799
  • 66
  • 75
  • 101
Anurag Daware
  • 441
  • 1
  • 5
  • 15
  • This used to be specific to Windows, but there is now an effort to make the `py` wrapper available for all platforms. If you don't have it yet, this answer won't help. – tripleee Jan 12 '23 at 06:22
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,929
  • 6
  • 66
  • 79
0

In the general case, you will need to find out the precise location of your Python 2 and Python 3 interpreters. On Windows, the whereis command will reveal where a program is installed; on Unix and related platforms, try type or command -v (don't use the popular but nonstandard tools whereis or which unless you know you can trust them).

Windows example:

C:\loser> whereis python
C:\Python37\Python.exe

Linux example:

bash$ type python
python is /usr/local/bin/python

For instance, you might have Python 3 in /usr/local/bin/python3 and Python 2 in /usr/bin/python.

(This is by no means universal; where and how you installed the different Python versions can vary enormously. You could have Python in /opt/anaconda/forest/trees/shirley for all I know. Probably also look in /opt and in your home directory, or just use the search facilities of your OS to see what's where.)

Generally, if you find a Python binary, /path/to/this/python --version will print its version number.

Once you know this, using the full path to the version you want to use will always get you precisely that version, with its specific configuration for where to install packages.

So, in this example,

/usr/local/bin/python3 -m pip install requests

will install requests for Python 3, and

/usr/bin/python -m pip install requests

for Python 2. But again, these are just examples; you need to know the correct paths for your specific system.

On Windows (and now, going forward, other platforms) there is a wrapper called py which lets you conveniently specify which version of Python you want to run with an option. py -2 runs Python 2 (provided you have installed it, of course), and you can probably guess how to get Python 3.

By the way; on Debian and derived Linux distros, including Ubuntu, Mint, etc, pip is not necessarily installed when you apt install python3 (or python etc); you will need to separately install python3-pip (or python-pip etc).

For convenience, you might want to explore using virtual environments to control your Python environment. With Python 3, you want /path/to/your/python -m venv envname to create envname which, when activated, will always run that particular Python version when you simply run python, and the correct corresponding pip when you type that command. (Again, on Debian-based distros, you will separately need to install python3-venv.) With Python 2, you need to separately install the third-party module virtualenv. (There are other tools with similar facilities but slightly different features; but these are de facto standard, pretty much.)

If there are only a couple of versions which you want to keep on using in parallel, you can create a wrapper or alias so you don't have to type the full path every time. For example, if $HOME/bin is on your PATH, you could create a file $HOME/bin/py3 with the contents

#!/bin/sh
exec /path/to/python3 "$@"

and chmod +x this file to be able to type just py3 going forward. (The syntax for a similar wrapper on Windows will be different; in cmd you want %* instead of "$@" and no exec or #!/bin/sh shebang, and probably a @ in front. There is no chmod on Windows, but the file needs to have the .cmd extension.)

Of course, if the versions you want to use already have different names, you just need to make sure their directories are on your PATH, and make sure no other programs with the same name are in a directory which is earlier on your PATH.

If you want to be able to use multiple Python versions in parallel (for example, a specific version for each project you are working on) there are third-party tools like pyenv which let you maintain a number of parallel Python installations and choose between them easily. But this is just a convenience; if you know what you are doing, you can always run a specific Python version with the instructions in this answer.

tripleee
  • 175,061
  • 34
  • 275
  • 318