I've read the documentation on egg entry points in Pylons and on the Peak pages, and I still don't really understand. Could someone explain them to me?
3 Answers
An "entry point" is typically a function (or other callable function-like object) that a developer or user of your Python package might want to use, though a non-callable object can be supplied as an entry point as well (as correctly pointed out in the comments!).
The most popular kind of entry point is the console_scripts
entry point, which points to a function that you want made available as a command-line tool to whoever installs your package. This goes into your setup.py
script like:
entry_points={
'console_scripts': [
'cursive = cursive.tools.cmd:cursive_command',
],
},
I have a package I've just deployed called cursive.tools
, and I wanted it to make available a "cursive" command that someone could run from the command line, like:
$ cursive --help
usage: cursive ...
The way to do this is define a function, like maybe a cursive_command
function in the file cursive/tools/cmd.py
that looks like:
def cursive_command():
args = sys.argv[1:]
if len(args) < 1:
print "usage: ..."
and so forth; it should assume that it's been called from the command line, parse the arguments that the user has provided, and ... well, do whatever the command is designed to do.
Install the docutils
package for a great example of entry-point use: it will install something like a half-dozen useful commands for converting Python documentation to other formats.

- 10,524
- 11
- 77
- 109

- 83,755
- 16
- 106
- 147
-
4the current docutils' `setup.py` doesn't contain `entry_points` at all. – matt wilkie May 19 '13 at 07:28
-
2This is an excellent answer as it demonstrates the power of multiple projects sharing a single entry_point group name, which is "console_scripts". Compare this answer to the more general answer by Petri. You'll see that setuptools must be using this pkg_resources mechanism to get the console_scripts and then create a shell wrapper around them. Inspiring? Use these. They are good for more than just console_scripts. – Bruno Bronosky Mar 11 '15 at 21:37
-
Could it be done if I install my project using ` pip install -e . --use-deprecated=legacy-resolver`? its folder path woudn't be created under site-packages – alper Dec 11 '21 at 15:19
EntryPoints provide a persistent, filesystem-based object name registration and name-based direct object import mechanism (implemented by the setuptools package).
They associate names of Python objects with free-form identifiers. So any other code using the same Python installation and knowing the identifier can access an object with the associated name, no matter where the object is defined. The associated names can be any names existing in a Python module; for example name of a class, function or variable. The entry point mechanism does not care what the name refers to, as long as it is importable.
As an example, let's use (the name of) a function, and an imaginary python module with a fully-qualified name 'myns.mypkg.mymodule':
def the_function():
"function whose name is 'the_function', in 'mymodule' module"
print "hello from the_function"
Entry points are registered via an entry points declaration in setup.py. To register the_function under entrypoint called 'my_ep_func':
entry_points = {
'my_ep_group_id': [
'my_ep_func = myns.mypkg.mymodule:the_function'
]
},
As the example shows, entry points are grouped; there's corresponding API to look up all entry points belonging to a group (example below).
Upon a package installation (ie. running 'python setup.py install'), the above declaration is parsed by setuptools. It then writes the parsed information in special file. After that, the pkg_resources API (part of setuptools) can be used to look up the entry point and access the object(s) with the associated name(s):
import pkg_resources
named_objects = {}
for ep in pkg_resources.iter_entry_points(group='my_ep_group_id'):
named_objects.update({ep.name: ep.load()})
Here, setuptools read the entry point information that was written in special files. It found the entry point, imported the module (myns.mypkg.mymodule), and retrieved the_function defined there, upon call to pkg_resources.load().
Calling the_function would then be simple:
>>> named_objects['my_ep_func']()
hello from the_function
Thus, while perhaps a bit difficult to grasp at first, the entry point mechanism is actually quite simple to use. It provides an useful tool for pluggable Python software development.
-
4Where is the 'my_ep_func' name used in all this process? It doesn't seem to be used for anything by the pkg_resources iterator. – Kamil Kisiel Apr 10 '12 at 22:56
-
2@KamilKisiel: in the example used for illustration here, the name of the entry point is indeed not used for anything, nor need it be; whether or not the name of the entry point is used for anything is up to the application. The name is available simply as the _name_ attribute of the entry point instance. – Petri Oct 04 '12 at 08:47
-
4I think that discarding the ep.name and making named_objects a list instead of a dictionary was confusing so I edited the answer. This what the answer shows both where to get the name and whether to expect it to be 'the_function' or 'my_ep_func'. Otherwise the reader had to find additional documentation elsewhere. This is an EXCELLENT answer and is the shortest, clearest explanation of entry_points that I have ever seen! – Bruno Bronosky Mar 11 '15 at 21:23
-
4I've created a project on github that demonstrates this concept. https://github.com/RichardBronosky/entrypoint_demo – Bruno Bronosky Mar 12 '15 at 06:41
-
1This is a very clear explanation of entry points, that you for the detailed explanation. The `EntryPoints` link is stale, though the explanation is very clear. – unrahul Aug 02 '16 at 19:29
-
From setuptools doc- "Attention: Use of pkg_resources is discouraged in favor of importlib.resources, importlib.metadata, and their backports (importlib_resources, importlib_metadata). Please consider using those libraries instead of pkg_resources." – Shihab Shahriar Khan Dec 20 '22 at 06:49
From abstract point of view, entry points are used to create a system-wide registry of Python callables that implement certain interfaces. There are APIs in pkg_resources to see which entry points are advertised by a given package as well as APIs to determine which packages advertise a certain entry point.
Entry points are useful for allowing one package do use plugins that are in another package. For instance, Ian Bicking's Paste project uses entry points extensively. In this case, you can write a package that advertises its WSGI application factory using the entry point paste.app_factory
.
Another use for entry points is enumerating all the packages on the system that provide some plugin functionality. The TurboGears web framework uses the python.templating.engines
entry point to look up templating libraries that are installed and available.

- 11,672
- 5
- 39
- 38