How to install pip for Python 2
Asked Answered
A

8

58

I run

python --version

and get

Python 2.7.3

I run

pip --version

and get

pip 1.5 from /usr/local/lib/python3.2/dist-packages/pip-1.5-py3.2.egg (python 3.2)

I installed pip using apt-get. How to I get the Python 2 version of pip?

I've reinstalled python and python-pip several times with apt-get. I'm also curious why these would install different Python versions.

Amenity answered 23/1, 2014 at 10:31 Comment(1)
This question is similar or duplicate for this when you try to install using apt-getDeprivation
K
88

To install pip for Python2 on Ubuntu, this worked for me

sudo apt update
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
sudo python2 get-pip.py

It's based on DareDevil7's answer but notice the url is different.

Karleenkarlen answered 20/3, 2021 at 7:10 Comment(3)
you don't need to sudo; installing in user-space is fineMistrial
This works even though pip officially dropped support for Python2.Delladelle
Amazing that people are still installing Python2 in 2024...Karleenkarlen
C
6

If there are both python2.7 and python3 in you ubuntu system,run this

sudo apt install python-pip

There will be pip for python3 ,pip2 for python2

Colorimeter answered 26/5, 2018 at 1:4 Comment(4)
Unable to locate package python-pipShaveling
This answer is incorrect now that python2.7 is out of support.Longford
Not found check your command again.Carolyn
This works beautifully for ubuntu:trusty, whereas python2 get-pip.py doesn't work (results in "ERROR: Could not find a version that satisfies the requirement pip<21.0 (from versions: none)" )Adenosine
R
6

run this

python2.7 -m ensurepip --upgrade
Rumpus answered 14/12, 2021 at 7:59 Comment(2)
Very useful. Needed pip to install virtualenv for supporting legacy code on AWS Linux. Also appreciated (a) no need for internet access (b) support of --user option to keep things isolated. docs.python.org/2.7/library/ensurepip.html#module-ensurepipChristabelle
Thank you. I think this is the only way to install pip2 using the python2.7 package from the distribution repository on modern Fedora Linux.Rickard
H
4

I would suggest that you use pyenv to manage multiple versions of Python, because it can often get problematic. Right now the solution to the problem would depend on the configuration you have for pip and python in your bash.

One thing you can do is download the easy_install script, and use python 3 to run it and install pip for python 3 alone.

Hawkeyed answered 23/1, 2014 at 10:39 Comment(0)
C
3

If you really want to install pip globally for your system use the get-pip.py script with the wanted python binary http://www.pip-installer.org/en/latest/installing.html#install-or-upgrade-pip

python2.7 get-pip.py

But you should consider using virtualenv / buildout to get an isolated environment

Crystalloid answered 23/1, 2014 at 10:50 Comment(3)
File "/tmp/tmpNoRNRb/pip.zip/pip/_internal/cli/main.py", line 60 sys.stderr.write(f"ERROR: {exc}") SyntaxError: invalid syntaxCanoewood
That syntax (f"ERROR: {exc}") is only in Python 3Postnatal
So that version of get-pip.py is for Python 3 and this answer is wrong, see Edward Gaere's answerPostnatal
D
2

Download the tar.gz of pip from https://pypi.python.org/pypi/pip#downloads.

Unzip or Untar, Then from its untar directory install for any specific version of python using

python2.7 setup.py install

or

python3.3 setup.py install
Deprivation answered 23/1, 2014 at 15:16 Comment(0)
P
2

Run the following Commands:

sudo add-apt-repository universe
sudo apt update
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
sudo python get-pip.py
Plonk answered 6/1, 2021 at 10:39 Comment(1)
Use bootstrap.pypa.io/pip/2.7/get-pip.py insteadAll
B
0

Its a not a good idea to install pip for python2 system wide. I guess, you need to run a project with python2. The best solution is vritualenv. I am making an assumption that in your installation which python2 returns /usr/bin/python2.7 and virtualenv --version returns virtualenv 20.15.1 from home/user/.local/lib/python2.7/site-packages/virtualenv/__init__.pyc

   1  virtualenv -p /usr/bin/python2.7 YOURPROJECT
   2. source YOURPROJECT/bin/activate

The last command will now activate the virtual environment and in this environment, your python is python2.7 and pip is installed for python2.7 as well. You can deactivate your virtual environment using deactivate.

Burkes answered 6/5, 2022 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.