Entry points are one of the most powerful, yet mysterious, ways to customise the behaviour of Python projects. With just a few lines of code in your
setup.py file, you can install command-line applications, inject features into other libraries, create plugins, and more! During this session, we will explore what entry points are, how they work, and how to use them in your Python projects.
We will start with a brief exploration of what entry points are, how they work, and what they can be used for
Next, we will look at 3 separate use cases:
Installing a command-line application on the $PATH: This is a great "entry level" (no pun intended) use case for entry points. We'll use setuptools to install a "Hello world" application, then demo a couple of well-known projects that use entry points to do the exact same thing, such as Django and PyOTA.
Defining a plugin interface: We will look at a command-line application that presents the user with a menu of commands that can be run. The goal is to install new menu commands from a completely separate Python project, without modifying a single line of code in the application. We will see how this can be done using entry points.
Creating a service registry: Let's up the ante a bit and look at a case where entry points may in fact be the only way to solve a problem elegantly. We will look at an application that relies on a Celery worker to process asynchronous tasks using a configurable helper class. However, because of the way Celery works, we cannot reference the helper class directly. We will evaluate a few typical approaches that can be taken to solve this problem – such as maintaining a global registry, or using a factory – but many of them introduce side effects or require extra work to maintain. Using entry points, we can resolve the issue easily with just a few lines of code.