From docs, entry points are used to define things that installers know about for example, cli main scripts or plugins

Entry points are a mechanism for an installed distribution to advertise components it provides to be discovered and used by other code. For example:

Distributions can specify console_scripts entry points, each referring to a function. When pip (or another console_scripts aware installer) installs the distribution, it will create a command-line wrapper for each entry point.

Applications can use entry points to load plugins; e.g. Pygments (a syntax highlighting tool) can use additional lexers and styles from separately installed packages. For more about this, see Creating and discovering plugins.

Console script Link to heading

The first application is probably the most common one, where setup.py defines console script, and when setup.py runs, i will install a wrapper around that cli.

    entry_points="""
        [console_scripts]
        foo=foobar.cli:cli
    """,

plugins Link to heading

Using importlib, we can look information about installed packages and get their entry points.


In [1]: from importlib import metadata

In [2]: metadata.version("wheel")
Out[2]: '0.37.1'

In [6]: d = metadata.distribution("wheel")

In [7]: d.entry_points
Out[7]:
[EntryPoint(name='wheel', value='wheel.cli:main', group='console_scripts'),
 EntryPoint(name='bdist_wheel', value='wheel.bdist_wheel:bdist_wheel', group='distutils.commands')]

So, how to define an entry point for a package?

First we need to define the entry points in setup.py, the entry points have groups and each group have set of points with name and value each.


from distutils.core import setup

setup(name = "mypkg",
        version = "100",
        description = "yadda yadda",
        author = "myself and I",
        author_email = "email@someplace.com",
        url = "whatever",
        long_description = """Really long text here.""",
        packages = ['tools'],
        entry_points={
            'console_scripts': [
                'cursive = tools.cmd:cursive_command',
            ],
            "llm" : ["mymod = tools.mymod"]
        },
)

Now, we just need to install the package

pip install .

with some code to get information

import importlib
from importlib import metadata
import os
import sys

package_name = "mypkg"
distribution = metadata.distribution(package_name)
entry_points = [ ep for ep in distribution.entry_points ]

print(entry_points)
for entry_point in entry_points:
    mod = entry_point.load()
    print(mod)

It will prints 2 entry points and, and i can load them directly, for example, the second entry point loads the module defined in value field.

[EntryPoint(name='cursive', value='tools.cmd:cursive_command', group='console_scripts'), EntryPoint(name='mymod', value='tools.mymod', group='llm')]
<function cursive_command at 0x7f9a183dd750>
from mymod
<module 'tools.mymod' from '..venv/lib/python3.10/site-packages/tools/mymod.py'>