How to install pip on Python 2.7 in 2021 - python-2.7

I have legacy production servers that are still running Python 2.7.6. We have a local environment built from the docker image for ubuntu 14.04 intended to replicate that environment (things still work there once everything is installed.) The packer build script that creates this environment recently stopped working apparently due to PyPi dropping non-SNI support.
I tried using get-pip.py from the docs to download pip:
wget -c https://bootstrap.pypa.io/pip/2.7/get-pip.py
python2 get-pip.py
This gives me the following warning:
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:424: SNIMissingWarning: An HTTPS request has been made, but the SNI (Server Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
/tmp/tmpBb3LJu/pip.zip/pip/_vendor/urllib3/util/ssl_.py:164: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ERROR: Could not find a version that satisfies the requirement pip<21.0 (from versions: none)
ERROR: No matching distribution found for pip<21.0
The proposed solution for that is to use pip to upgrade urllib3
https://serverfault.com/questions/866062/easy-install-and-pip-fail-with-ssl-warnings
I don't have pip so I installed a legacy version using
apt-get install python-pip
This installs pip 1.5.4
When I try to pip install "urllib3[secure]" I get the following:
Requirement already satisfied (use --upgrade to upgrade): urllib3[secure] in /usr/lib/python2.7/dist-packages
Installing extra requirements: 'secure'
Cleaning up...
If I try pip install "urllib3[secure]" --upgrade or pip install --index-url https://pypi.python.org/simple/ --upgrade pip I get:
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement urllib3[secure] in /usr/lib/python2.7/dist-packages
Downloading/unpacking urllib3[secure]
Cleaning up...
No distributions at all found for urllib3[secure] in /usr/lib/python2.7/dist-packages
Storing debug log for failure in /root/.pip/pip.log
(the pip message reflects pip, not urllib3[secure])
When I try to use pip 1.5 to install uWSGI
pip install uWSGI
I get the following:
Downloading/unpacking uWSGI
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement uWSGI
Cleaning up...
No distributions at all found for uWSGI
Storing debug log for failure in /root/.pip/pip.log
Upgrading pip doesn't work here either
Downloading/unpacking uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Cleaning up...
No distributions at all found for uWSGI==2.0.18 (from -r /root/requirements.txt (line 1))
Storing debug log for failure in /root/.pip/pip.log
Reinstalling pip doesn't work:
python -m pip install -U --force-reinstall pip
Gives me:
Downloading/unpacking pip
Cannot fetch index base URL https://pypi.python.org/simple/
Could not find any downloads that satisfy the requirement pip
Cleaning up...
No distributions at all found for pip
Storing debug log for failure in /root/.pip/pip.log
If I open /root/.pip/pip.log I see the following:
Downloading/unpacking pip
Getting page https://pypi.python.org/simple/pip/
Could not fetch URL https://pypi.python.org/simple/pip/: 403 Client Error: [[[!!! BREAKING CHANGE !!!]]] Support for clients that do not support Server Name Indication is temporarily disabled and will be permanently deprecated soon. See https://status.python.org/incidents/hzmjhqsdjqgb and https://github.com/pypa/pypi-support/issues/978 [[[!!! END BREAKING CHANGE !!!]]]
The link says that SNI support was dropped:
For users of Python 2.7.{0...8}
Upgrading to the last Python 2.7 release is an option.
However, note that Python 2.7 series itself is now End of Life and support in pip was dropped with version 21.0.
For users of Python 2.6.x and lower:
Neither the Python core developers, or pip maintainers support Python 2.6 and below.
If someone is aware of a work around for this issue (SNI support specifically) they are welcome to share it here for others.
There is no recommended solution from the PyPI team.
How can I get a local environment set up for new developers to work on our legacy application? I've created a new Python 3 dev server and local environment but it will be some time before I can roll out the staging and live environments, get everything moved over, and test it.

As the message says, PyPi has discontinued support for Python <2.7.9 as of May 6th 2021. If you're running a version < 2.7.9 and you cannot upgrade to a newer version of Python then your only option is to manually download the wheels from PyPi.
These are the modification I needed to make to my build script to make it work:
I needed to install software-properties-common and gcc
apt-get install -y software-properties-common gcc
Then I downloaded (setuptools](https://pypi.org/project/setuptools/44.1.1/#files) and unzipped and installed it:
python ./setuptools-44.1.1/setup.py install
Next, I downloaded pip and added it to a folder called wheels. Then I could use the whl file to run pip to get pip
python ./wheels/pip-20.3.4-py2.py3-none-any.whl/pip install --no-index --find-links ./wheels/ pip --ignore-installed
It was suggested to build a Docker container using Ubuntu 16.04 with Python 2.7.17 and use that to download the packages.
pip download -r requirements.txt
But the versions of the packages were wrong, so I ended up going through the requirements.txt and downloading each package manually from PyPi and adding it to the wheels folder. A running instance is useful so you can run pip freeze or look at a requirements.txt file to grab the version numbers of all the packages you need.
Now that I could use pip, I can install my other packages:
python pip install --no-index --find-links ./wheels/ -r /root/requirements.txt
This uncovered some dependencies that I hadn't downloaded packages for yet so I had to go through and download those and added them to the wheels folder. There were a few other things I found needed different versions than I had originally downloaded and a few packages relied on pbr and many more wanted wheel:
pip install --no-index --find-links ./wheels/ pbr==5.5.1 wheel==0.36.2
I also needed to download cMake and add it to the wheels folder
After that I could install my requirements.txt:
pip install --no-index --find-links ./wheels/ -r /root/requirements.txt --ignore-installed

May be late to the party but something similar happened to me while trying to make an HTTPS request with Python 2.7.6 (lack of SNI support). This was causing a lot of issues on a remote web server I work on.
Looking for answers I tried installing urllib3[secure] and entered a loophole since pip was complaining about a lack of SNI support to install this and other packages as well.
I found out this StackOverflow answer which helped me install the required dependencies to make Python 2.7.6 and pip itself support SNI as well as install urllib[secure].
You need to create a folder containing the required wheels (download them from PyPi using wget for instance):
pip, asn1crypto, enum34, idna, six, ipaddress, pyOpenSSL, cffi,
cryptography wheels; and also pycparser (a non-wheel, it will be a
tar.gz)
Make sure the wheels you download support Python 2.7 and that you install pip before the rest of them.
In the original answer, its stated you can use python -m OpenSSL.debug to verify everything worked correctly (a ModuleNotFoundError would mean the pyOpenSSL package was not installed). You can also use pip -Vto check that the new pip version was installed correctly as well.
After updating pip and installing these dependencies I was able to install urllib3[secure] and get SNI support from python as well as pip.
Good luck!

I am sharing this answer as an update to Jonathan Rys's answer that contains the steps required as of the date of this answer. I tried to keep this concise.
As the message says, PyPi has discontinued support for Python <2.7.9 as of May 6th 2021. If you're running a version < 2.7.9 and you cannot upgrade to a newer version of Python then your only option is to manually download the wheels from PyPi.
For Ubuntu 20.04, I have installed build-essential sudo apt-get install build-essential
I installed Python 2 from source, downloading tar bundle and built and installed this. Note, I removed the python command, to avoid that old confusion, so we have python2 and python2.7 and also python3 (for example).
tar xf Python-2.7.18.tgz
cd Python-2.7.18
./configure && make && sudo make install
(cd /usr/local/bin;sudo rm python python-config)
cd ..
Now to get pip installed. Download setuptools zip archive. Then install it:
unzip setuptools-44.1.1.zip
cd setuptools-44.1.1
python2 bootstrap.py
sudo python2 setup.py install
cd ..
Next, I downloaded pip .tar.gz archive. Then unpack and install it. Note, I took extra steps to preserve and restore the original python3 pip in /usr/local/bin. Pip for Python2 are still available as pip2 and pip2.7 in the same directory.
tar xf pip-20.3.4.tar.gz
cd pip-20.3.4
(cd /usr/local/bin;sudo mv pip pip-save)
sudo python2 setup.py install
(cd /usr/local/bin;sudo mv pip-save pip)
Now that I could use pip, I can install the most important package any Python user should install, ipython. Note that I do a user install for this and also preserve my "ipython" command to run python3 and have ipython2 to run python2:
pip2.7 install ipython
(cd ~/.local/bin;rm ipython;ln -s ipython3 ipython)
and it works!
$ ipython2
Python 2.7.18 (default, Jun 22 2022, 09:38:45)
Type "copyright", "credits" or "license" for more information.
IPython 5.10.0 -- An enhanced Interactive Python.

Related

Trying to install Pyramid package with "python setup.py install" gives error searching for a package

I'm working on a Pyramid project that has to be deployed in a Debian 7 server.
The Debian 7 server has the python version 2.7.3 as from the archives repositories.
After creating a virtual environment with virtualenv command the pip version installed in this virtualenv is 1.1.
First thing noted: I can't upgrade pip with pip install --upgrade pip. The version is not updated and remains in 1.1 version.
After installing the OS packages needed to install the project I ran python setup.py install but I get the error:
Searching for zipp==0.5
Reading http://pypi.python.org/simple/zipp/
Couldn't find index page for 'zipp' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading http://pypi.python.org/simple/
No local packages or download links found for zipp==0.5
error: Could not find suitable distribution for Requirement.parse('zipp==0.5')
So, the python setup.py install command could not recognize the zipp package.
I thought that this was related to the older version of pip (1.1). So I could update pip version successfully using the index-url option:
$ pip install --index-url https://pypi.python.org/simple --upgrade pip
That updated pip version from 1.1 to 20.3b1. Then I tried python setup.py install again, but the same error occurred.
What I could see is that, after updating pip version, the zipp==0.5.0 package is installed if I ran pip install zipp==0.5.
I'm new to Pyramid and the package installing using the setup.py module. I'm not understanding why I can install the zipp package with pip and can't with python setup.py install.
I'm not putting the content of setup.py here because I think it's not a problem of the setup.py script. With more recent versions of python and pip in an Ubuntu 18.04 machine the setup.py works like a charm.
Someone could explain how to solve this issue?
When you run python setup.py install directly, you are not using pip, so the version of pip you have installed is irrelevant. This command is antiquated, should not be used, etc, etc. The right answer is to use pip install . as a replacement for python setup.py install and to use pip install -e . as a replacement for python setup.py develop.
With respect to what you're seeing, when you run python setup.py install this using the version of easy_install bundled with setuptools to talk to PyPI and install dependencies. This should be avoided for a lot of reasons but just know that to override urls it uses they go in ~/.pydistutils.cfg and have nothing to do with pip. HTTPS is one problem that you looked at, another is that old versions of Python do not have the right CA trust store, nor support the minimum required TLS 1.2 to handshake with PyPI. So expect lots of problems using old tools - at the very least try to use pip instead of easy_install.

How to install Pip on a new Ubuntu upgrade

I posted the question below, but none of the answers I was pointed to worked, though they look like they should.
I activated (again) the virtualenv. It still tells me that pip can't be found by apt when doing an 'apt install' command. But here is where I am now, and very confused.
I pointed my directory to "/home/.../q7root/bin/pip" and did an "ls". It shows a sub-directory with pip in it (or, I think, a link to it - I'm not the best at Unix). When I type "which pip" I get the path to this point ('q7root/pip'). bit if I just type "pip" at the CLI I get I get this error:
[![pip error][1]][1]
I have looked at my PATH, and this q7root/bin is the first place to look on the path. And, despite trying mightily with all the references people gave me, pip3 never gets installed.
But even pip is challenged. "which pip" points to this copy in the virtual environment site, but typing "pip" as a command tells me 'No module named pip.'
So pip seems to need more stuff installed (?), or there is some mess. Any advice?
Original Question:
At the suggestion of others working on what was a functional Django project, I upgraded to a more recent version of Ubuntu (18).
However, when I first try to run it it blows up at line 3 of the initial script module when asked to import django as a package.
I tried pip -r requirements.txt, but the system said pip was an unknown package. I dropped down and used apt to load pip onto my machine (sudo apt-get pip), then tried using pip itself (pip update pip) which failed.:
[![Pip load error message][2]][2]
I also tried pip install django, and got this:
[![django not found][3]][3]
I would have thought an OS upgrade would not require re-installing all currently installed packages (seems like a no-brainer to do the work of installing everything that had been installed). But right now I am terribly stuck...obviously, having 'pip' let's you (at least) have a basic CLI tool.
Any advice?
[1]: https://i.stack.imgur.com/OPfgc.png
[2]: https://i.stack.imgur.com/shLOc.png
[3]: https://i.stack.imgur.com/bEhDB.png
It depends on the version of python.
Python 3
sudo apt update
sudo apt install python3-pip
Python 2
sudo apt update
sudo apt install python-pip
How to Install Pip on Ubuntu 18.04
Start with a fresh Ubuntu install. I think you've run too many commands for your current setup to be reproduceable.
Install python3 and python3-venv.
sudo apt update
sudo apt install python3 python3-venv
Use the venv module to create the virtual env.
python3 -m venv myenv
source myenv/bin/activate
You now have access to pip in the venv.
It's OK to upgrade pip in the virtual env, I suggest you don't ever upgrade the system pip otherwise you might hit issues like this.
(myenv) python -m pip --version
(myenv) python -m pip install --upgrade pip
Now you can use pip to install your requirements in the virtual env.
(myenv) python -m pip install -r requirements.txt
In the above commands I've used python -m pip instead of pip. This is the recommended way, as it ensures that you are using the version of pip that matches python.
In the end, this was a state of deep computer confusion. I was already disk-limited so I bought a new computer, and this error did not recur with the same code being used.

django update version without pip

I want to update my django version (1.8 ->1.9).
My current version was installed using pip (pip install Django) .
Now I need to install a newer version without pip.
I think I should run from the django package folder, using the setup.py file:
python setup.py install
[I've installed some other packages using a setup.py file .]
The question - will that create some problems? And, does the setup.py install method take care for uninstalling the older version? Or I have to uninstall manually somehow?
I don't have internet connection in this computer, so no pip.
Download a release (they are available on GitHub), and copy to your computer.
Then use pip install to install the archive. For example
pip install django-1.9.3.tar.gz
For more ways to use pip install, see the docs.

pyvenv & pip not installing into local site-packages

I'm test driving the Django 1.6b, Python 3.3.2 (compiled from source) and pyvenv with Ubuntu 12.04.
Every time I try and install perform a pip install [package] the package attempts to install itself globally rather than into my local environment. A simple workflow is as follows:
$ pyvenv environments/roebk
$ source environments/roebk/bin/activate
$ (roebk) pip install south
error: could not create '/usr/local/lib/python3.3/site-packages/south': Permission denied
I've double checked that I'm using the correct version of pip.
$ pip -V
pip 1.4 from /usr/local/lib/python3.3/site-packages/pip-1.4-py3.3.egg (python 3.3)
Am I missing anything obvious?
Did you install setuptools and pip into the environment? virtualenv installs setuptools and pip automatically into a new environment.
$ virtualenv qwerty
New python executable in qwerty/bin/python
Installing setuptools............done.
Installing pip...............done.
$
According to the pyvenv docs you need to install them into the new environment manually.
Common installation tools such as Distribute and pip work as expected
with venvs - i.e. when a venv is active, they install Python packages
into the venv without needing to be told to do so explicitly. Of
course, you need to install them into the venv first: this could be
done by running distribute_setup.py with the venv activated, followed
by running easy_install pip. Alternatively, you could download the
source tarballs and run python setup.py install after unpacking, with
the venv activated.
Upon the official docs I thought Python 3.4 would install pip automatically, but it seems, it doesn't:
Changed in version 3.4: Installs pip by default, added the --without-pip and --copies options
EDIT: Somehow I managed to use a Python3.3.2 version also installed on that machine. With Python3.4, it works as expected.

Installing MySQL-python for Django

I've just learned how to use virtualenv and I installed Django 1.4.5. I'm assuming that the virtualenv created a clean slate for me to work on so with the Django 1.4.5 installed, I copied all my previous files into the virtualenv environment.
I tried to run the server but I get an error saying "no module named MySQLdb". I think this means that I forgot to install MySQL-python. I tried to install it via
pip install MySQL-python
But I get this error
Downloading/unpacking MySQL-python
Running setup.py egg_info for package MySQL-python
The required version of distribute (>=0.6.28) is not available,
and can't be installed while this script is running. Please
install a more recent version first, using
'easy_install -U distribute'.
(Currently using distribute 0.6.24 (/home/bradford/Development/Django/django_1.4.5/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg))
Complete output from command python setup.py egg_info:
The required version of distribute (>=0.6.28) is not available,
and can't be installed while this script is running. Please
install a more recent version first, using
'easy_install -U distribute'.
(Currently using distribute 0.6.24 (/home/bradford/Development/Django/django_1.4.5/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg))
----------------------------------------
Command python setup.py egg_info failed with error code 2 in /home/bradford/Development/Django/django_1.4.5/build/MySQL-python
Not quite sure how to go about fixing this problem =/ any help much appreciated!
I recently had exactly this issue (just not in relation to Django). In my case I am developing on Ubuntu 12.04 using the default pip and distribute versions, which are basically a little out of date for MySQL-python.
Because you are working in an isolated virtualenv, you can safely follow the suggested instruction without affecting your Python installation.
So you can...
workon your_virtualenv #activate your virtualenv, you do use virtualenvwrapper, right?
easy_install -U distribute #update distribute on your virtualenv
pip install MySQL-python #install your package
If for some reason upgrading distribute is not an option, you could try installing an older version of MySQL-python as follows (you'd have to check this version is compatible with your version of Django):
pip install MySQL-python==x.y.z #where x.y.z is the version you want
Spent an hour looking through stackoverflow. Evntually found answer in the other question. This is what saved me:
sudo apt-get install libmysqlclient-dev
mysql_config goes with the package.
When doing in a virtualenv :
pip install MySQL-python
I got
EnvironmentError: mysql_config not found
To install mysql_config, as Artem Fedosov said, first install
sudo apt-get install libmysqlclient-dev
then everything works fine in virtualenv
MySQL driver for Python (mysql-python) needs libmysqlclient-dev. You can get it with:
sudo apt-get update
sudo apt-get install libmysqlclient-dev
If python-dev is not installed, you may have to install it too:
sudo apt-get install python-dev
Now you can install MySQL driver:
pip install mysql-python
Here is a more detailed documentation for MySQL in Django:
http://codex.themedelta.com/how-to-install-django-with-mysql-in-a-virtualenv-on-linux/
I had to do this:
pip install mysql-python
inside the virtualenv
The commands are always run in ubuntu:
easy_install -U distribute
later
sudo apt-get install libmysqlclient-dev
and finally
pip install MySQL-python
The suggested solutions didn't work out for me, because I still got compilation errors after running
`$ sudo apt-get install libmysqlclient-dev`
so I had to run
apt-get install python-dev
Then everything worked fine for me with
apt-get install python-dev
Try this:
Version Python 2.7
MySQL-python package, you should use either MySQL_python‑1.2.5‑cp27‑none‑win32.whl or
MySQL_python‑1.2.5‑cp27‑none‑win_amd64.whl depending on whether you have installed 32-bit or 64-bit Python.
pip install MySQL_python‑1.2.5‑cp27‑none‑win32.whl
if you are using mysqlclient package, then use
mysqlclient‑1.4.6‑cp27‑cp27m‑win32.whl or
mysqlclient‑1.4.6‑cp27‑cp27m‑win_amd64.whl
pip install mysqlclient‑1.4.6‑cp27‑cp27m‑win32.whl
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient