Skip to content

Instantly share code, notes, and snippets.

@cmuller
Last active November 29, 2020 21:02
Show Gist options
  • Save cmuller/ec7733704c3ac6744071c6f727b19a6a to your computer and use it in GitHub Desktop.
Save cmuller/ec7733704c3ac6744071c6f727b19a6a to your computer and use it in GitHub Desktop.
How to use Requests in python2.7 in Ubuntu 20.04

How to use Requests in python2.7 in Ubuntu 20.04

python-request package has been dropped in Ubuntu 20.04

According to https://packages.ubuntu.com/eoan/python-requests, the python-request package has been dropped after Ubuntu Eoan (19.10) => The package is no more available on Ubuntu focal (20.04)

As a consequence you can have legacy Python2 code that ends up with this type of error: (I assume that you have the python2-minimal package installed)

Python 2.7.18rc1 (default, Apr  7 2020, 12:05:55) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests

How to solve this

  • best solution: port your code to Python3 :-) but the amount of effort can vary, and you are judge of how and when to do that
  • solution I don't like: pip install the package: 99% of the solutions on the web mention this but IMHO it is not clean and can lead to issues, unless you use virtualenvs, pipenv, or docker etc.
  • quick and easy solution: simply use the python3 package

Use the python3 package method

If you read the readthedocs site for Request, http://python-requests.org/ ( = https://requests.readthedocs.io/en/master/), they say:

Requests officially supports Python 2.7 & 3.4–3.7, and runs great on PyPy.

=> it is enough to install on Ubuntu 20.04 the python3-requests package (which is probably already present on your system), then you can look at where the library has been installed:

$ dpkg -L python3-requests | grep __init__.py
/usr/lib/python3/dist-packages/requests/__init__.py

and either:

  • create a sumbolic link from the python3 library zone to the python2 (probably /usr/lib/python2.7/dist-packages), or
  • add this line to your legacy code:
Python 2.7.18rc1 (default, Apr  7 2020, 12:05:55) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/usr/lib/python3/dist-packages')
>>> import requests
>>> ^D 

Be aware however that simply doing this can be dangerous: some of the packages can be missing from the python2 library zone, taken from python3 and.. not be compatible! => you will have to check that your legacy application is working as intended.

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment