Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Python Automation Cookbook
  • Table Of Contents Toc
Python Automation Cookbook

Python Automation Cookbook - Third Edition

By : Jaime Buelta
close
close
Python Automation Cookbook

Python Automation Cookbook

By: Jaime Buelta

Overview of this book

Automating repetitive tasks and integrating systems efficiently becomes increasingly complex as workflows scale. This book helps you solve that problem with practical Python recipes that guide you from foundational automation to advanced, AI-powered workflows. You start by building a strong base in Python automation, exploring tested solutions for file handling, web scraping, APIs, testing, and system operations, and learning how to design reliable automation workflows. The cookbook approach enables you to quickly apply solutions to real problems while building a deeper understanding through hands-on practice. This third edition expands the scope of automation by introducing AI-powered capabilities. You learn how to call AI models within your scripts, use and implement the Model Context Protocol (MCP) for system integration, and design intelligent agents that automate decision-making processes. New chapters provide real-world examples of AI agents in business automation, helping you move beyond scripts to adaptive systems. This book combines practical knowledge with modern techniques to ensure you stay current with evolving automation practices. By the end of this book, you will be able to design, build, and extend Python automation workflows, including AI-driven solutions, to handle complex real-world tasks with confidence.
Table of Contents (20 chapters)
close
close
18
Other Books You May Enjoy
19
Index

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/latest/.

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.32.5, 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 pendulum module for time handling (version 3.2.0: https://pendulum.eustace.io).

How to do it…

Create a requirements.txt file in our main directory, which will specify all the requirements for our project. Let's start with pendulum and requests:

pendulum==3.2.0
requests==2.32.5

Install all the requirements with the uv pip command:

$ uv pip install -r requirements.txt
Resolved 10 packages in 385ms
Installed 10 packages in 55ms
 + certifi==2026.5.20
 + charset-normalizer==3.4.7
 + idna==3.18
 + parse==1.20.2
 + pendulum==3.2.0
 + python-dateutil==2.9.0.post0
 + requests==2.32.5
 + six==1.17.0
 + tzdata==2026.2
 + urllib3==2.7.0

If you have previously used pip to install packages, you may be finding out that using uv is much faster.

Show the available modules installed using uv pip list:

$ uv pip list
Package            Version
------------------ -----------
certifi            2026.5.20
charset-normalizer 3.4.7
idna               3.18
parse              1.20.2
pendulum           3.2.0
python-dateutil    2.9.0.post0
requests           2.32.5
six                1.17.0
tzdata             2026.2
urllib3            2.7.0

You can now use both modules when using the virtual environment:

$ uv run python
Python 3.14.0 (main, Oct  7 2025, 09:34:52) [Clang 17.0.0 (clang-1700.3.19.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pendulum
>>> import requests
>>>

How it works…

The requirements.txt file specifies the module and version, and uv 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:

$ uv 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:

$ uv pip install -r requirements.txt

This is also applicable when a new module needs to be included.

At any point, the pipfreeze 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:

$ uv 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://github.com/vinta/awesome-python), which covers a lot of great tools for common Python use cases, such as cryptography, database access, date and time handling, and more.

GenAI can help with finding a suitable module for a particular use case.

Generally, it will be knowledgeable and direct you towards using common packages, such as requests. But you can ask about specific use cases and recommendations. Don't take a GenAI recommendation at face value, however: check the project itself and verify that it's what you need. A common problem from GenAI is that it will use an stablished older package that perhaps is not the best for your specific usage, for example, because there's a newer package that will get you better results. GenAI can help with the search, but not take the decision for you.

In some cases, installing packages may require additional tools, such as compilers or a specific library that supports specific functionality (for example, a particular database driver). If that's the case, the documentation will explain the dependencies.

See also

  • The Working with multiple Python versions and virtual environments with uv recipe, earlier in this chapter.
  • The Using a third-party tool—parse recipe, later in this chapter, to learn how to use one installed third-party module.
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Python Automation Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon