How to use pip with Python 3.x alongside Python 2.x

241,756

Solution 1

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.

Solution 2

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.

Solution 3

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>

Solution 4

The shortest way:

python3 -m pip install package
python -m pip install package

Solution 5

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
Share:
241,756

Related videos on Youtube

theta
Author by

theta

Updated on March 22, 2022

Comments

  • theta
    theta about 2 years

    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?

    • jpmc26
      jpmc26 over 10 years
      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.
  • theta
    theta almost 12 years
    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! :)
  • Piotr Dobrogost
    Piotr Dobrogost over 10 years
    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.
  • Shailen
    Shailen almost 10 years
    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.
  • dranxo
    dranxo almost 10 years
    On 12.04 I can't do this.
  • Tom Slee
    Tom Slee over 9 years
    I successfully used this (ie sudo apt-get install python3-pip) and then could install python3 packages using "sudo python3 -m pip install package".
  • Jon Surrell
    Jon Surrell about 9 years
    [x]Ubuntu is also using [ pip2 | pip2.7 | pip3 | pip3.4 ]. Is this a change in pip or different between different systems?
  • Yibo Yang
    Yibo Yang over 8 years
    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.
  • Kevin Brotcke
    Kevin Brotcke over 8 years
    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
  • Lennart Regebro
    Lennart Regebro over 8 years
    So do that then. :-)
  • Radagast
    Radagast over 7 years
    the same on voidlinux
  • Daniel Andrei Mincă
    Daniel Andrei Mincă almost 7 years
    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.
  • Lucas Spangher
    Lucas Spangher almost 6 years
    FYI, Mac UNIX also uses [ pip2 | pip2.7 | pip3 | pip3.4 ]
  • James Ray
    James Ray over 5 years
    Manjaro / Arch Linux also uses pip#.# (i.e. without the hyphen).
  • gebbissimo
    gebbissimo about 5 years
    that seems the most intuitive and easy solution to me, thanks!
  • Florent
    Florent about 5 years
    must be root to install python3-pip
  • axd
    axd over 4 years
    ``` (test-exif) alex@alex-xps-13-9370:~/projects/oe/app/exif$ python3 -m pip install exif /usr/bin/python3: No module named pip ```
  • fiveelements
    fiveelements over 4 years
    @axd in that case first install pip using sudo apt-get install python3-pip. Assuming you are using python3.
  • Max
    Max over 4 years
    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.
  • Curious Developer
    Curious Developer over 4 years
    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?
  • 林果皞
    林果皞 almost 4 years
    Warning for the comment above, never use sudo to install arbitrary python packages. See the risks here
  • Eduardo Pignatelli
    Eduardo Pignatelli almost 4 years
    This question doesn't have a unix tag. What about windows?
  • Shell_Leko
    Shell_Leko over 3 years
    On WSL I get ModuleNotFoundError: No module named 'apt_pkg'