218

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?

1
  • 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, 2013 at 6:38

11 Answers 11

206

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.

10
  • 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, 2012 at 8: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, 2014 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? Feb 18, 2015 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 Feb 1, 2016 at 4:39
  • So do that then. :-) Feb 3, 2016 at 11:44
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.

7
  • 2
    On 12.04 I can't do this.
    – dranxo
    Aug 5, 2014 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, 2014 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 3, 2015 at 6:29
  • must be root to install python3-pip
    – Florent
    Apr 25, 2019 at 9: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? Nov 26, 2019 at 13:26
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>
2
  • 7
    There's no Python 2 pip, that depends on the OS you're using. For example on Arch Linux pip --version states that's running on python 3.6 by default. May 18, 2017 at 9:12
  • that seems the most intuitive and easy solution to me, thanks!
    – gebbissimo
    Mar 12, 2019 at 9:13
30

The shortest way:

python3 -m pip install package
python -m pip install package
3
  • ``` (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, 2019 at 13:52
  • @axd in that case first install pip using sudo apt-get install python3-pip. Assuming you are using python3. Aug 19, 2019 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 at 6: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
0
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

0
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.

4

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

1
  • the same on voidlinux
    – Radagast
    Nov 27, 2016 at 8:17
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
    
1
  • 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 at 6: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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.