- Python Automation Cookbook
- Jaime Buelta
- 205字
- 2021-06-30 14:52:39
Installing third-party packages
One of the strongest capabilities of Python is the ability to use an impressive catalog of third-party packages that cover an amazing amount of ground in different areas, from modules specialized in performing numerical operations, machine learning, and network communications, to command-line convenience tools, database access, image processing, and much more!
Most of them are available on the official Python Package Index (https://pypi.org/), which has more than 200,000 packages ready to use. In this book, we'll install some of them. In general, it's worth spending a little time researching external tools when trying to solve a problem. It's very likely that someone else has already created a tool that solves all, or at least part, of the problem.
More important than finding and installing a package is keeping track of which packages are being used. This greatly helps with replicability, meaning the ability to start the whole environment from scratch in any situation.
Getting ready
The starting point is to find a package that will be of use in our project.
A great one is requests
, a module that deals with HTTP
requests and is known for its easy and intuitive interface, as well as its great documentation. Take a look at the documentation, which can be found here: https://requests.readthedocs.io/en/master/.
We'll use requests
throughout this book when dealing with HTTP
connections.
The next step will be to choose the version to use. In this case, the latest (2.22.0, at the time of writing) will be perfect. If the version of the module is not specified, by default it will install the latest version, which can lead to inconsistencies in different environments as newer versions are released.
We'll also use the great delorean
module for time handling (version 1.0.0: http://delorean.readthedocs.io/en/latest/).
How to do it…
- Create a
requirements.txt
file in ourmain
directory, which will specify all the requirements for our project. Let's start withdelorean
andrequests
:delorean==1.0.0 requests==2.22.0
- Install all the requirements with the
pip
command:$ pip install -r requirements.txt ... Successfully installed babel-2.8.0 certifi-2019.11.28 chardet-3.0.4 delorean-1.0.0 humanize-0.5.1 idna-2.8 python-dateutil-2.8.1 pytz-2019.3 requests-2.22.0 six-1.14.0 tzlocal-2.0.0 urllib3-1.25.7
Show the available modules installed using
pip list
:$ pip list Package Version --------------- ---------- Babel 2.8.0 certifi 2019.11.28 chardet 3.0.4 Delorean 1.0.0 humanize 2.0.0 idna 2.8 pip 19.2.3 python-dateutil 2.8.1 pytz 2019.3 requests 2.22.0 setuptools 41.2.0 six 1.14.0 tzlocal 2.0.0 urllib3 1.25.8
- You can now use both modules when using the virtual environment:
$ python Python 3.8.1 (default, Dec 27 2019, 18:05:45) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import delorean >>> import requests
How it works…
The requirements.txt
file specifies the module and version, and pip performs a search on pypi.org.
Note that creating a new virtual environment from scratch and running the following will completely recreate your environment, which makes replicability very straightforward:
$ pip install -r requirements.txt
Note that step 2 of the How to do it section automatically installs other modules that are dependencies, such as urllib3
.
There's more…
If any of the modules need to be changed to a different version because a new version is available, change them using requirements
and run the install
command again:
$ pip install -r requirements.txt
This is also applicable when a new module needs to be included.
At any point, the freeze
command can be used to display all of the installed modules. freeze
returns the modules in a format compatible with requirements.txt
, making it possible to generate a file with our current environment:
$ pip freeze > requirements.txt
This will include dependencies, so expect a lot more modules in the file.
Finding great third-party modules is sometimes not easy. Searching for specific functionality can work well, but, sometimes, there are great modules that are a surprise because they do things you never thought of. A great curated list is Awesome Python (https://awesome-python.com/), which covers a lot of great tools for common Python use cases, such as cryptography, database access, date and time handling, and more.
In some cases, installing packages may require additional tools, such as compilers or a specific library that supports some functionality (for example, a particular database driver). If that's the case, the documentation will explain the dependencies.
See also
- The Activating a virtual environment recipe, covered earlier in this chapter.
- The Using a third-party tool—parse recipe, covered later in this chapter, to learn how to use one installed third-party module.