How to use pip with Python 3.x alongside Python 2.x
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
Related videos on Youtube
theta
Updated on March 22, 2022Comments
-
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 over 10 yearsPersonally, 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 almost 12 yearsThis was easy, thanks.
sudo python3.2 get-pip.py
installspip
andpip-3.2
scripts in/usr/local/bin
and both logically use Python 3.sudo python get-pip.py
installspip
andpip-2.7
here, so in this casepip
uses Python 2.7. I additional created link topip-3.2
aspip3
and tested: Perfect! :) -
Piotr Dobrogost over 10 yearsCould 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 almost 10 yearsOn 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 almost 10 yearsOn 12.04 I can't do this.
-
Tom Slee over 9 yearsI 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 about 9 years[x]Ubuntu is also using
[ pip2 | pip2.7 | pip3 | pip3.4 ]
. Is this a change inpip
or different between different systems? -
Yibo Yang over 8 yearsAs 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 over 8 yearsAfter 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 over 8 yearsSo do that then. :-)
-
Radagast over 7 yearsthe same on voidlinux
-
Daniel Andrei Mincă almost 7 yearsThere'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 almost 6 yearsFYI, Mac UNIX also uses [ pip2 | pip2.7 | pip3 | pip3.4 ]
-
James Ray over 5 yearsManjaro / Arch Linux also uses
pip#.#
(i.e. without the hyphen). -
gebbissimo about 5 yearsthat seems the most intuitive and easy solution to me, thanks!
-
Florent about 5 yearsmust be root to install
python3-pip
-
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 over 4 years@axd in that case first install pip using
sudo apt-get install python3-pip
. Assuming you are using python3. -
Max over 4 yearsThis 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 over 4 yearsDespite of installing
pip
usingpython3.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 yearsWarning for the comment above, never use
sudo
to install arbitrary python packages. See the risks here -
Eduardo Pignatelli almost 4 yearsThis question doesn't have a
unix
tag. What about windows? -
Shell_Leko over 3 yearsOn WSL I get ModuleNotFoundError: No module named 'apt_pkg'