|
| 1 | +========================== |
| 2 | +Entry points specification |
| 3 | +========================== |
| 4 | + |
| 5 | +*Entry points* are a mechanism for an installed distribution to advertise |
| 6 | +components it provides to be discovered and used by other code. For |
| 7 | +example: |
| 8 | + |
| 9 | +- Distributions can specify ``console_scripts`` entry points, each referring to |
| 10 | + a function. When *pip* (or another console_scripts aware installer) installs |
| 11 | + the distribution, it will create a command-line wrapper for each entry point. |
| 12 | +- Applications can use entry points to load plugins; e.g. Pygments (a syntax |
| 13 | + highlighting tool) can use additional lexers and styles from separately |
| 14 | + installed packages. For more about this, see |
| 15 | + :doc:`/guides/creating-and-discovering-plugins`. |
| 16 | + |
| 17 | +The entry point file format was originally developed to allow packages built |
| 18 | +with setuptools to provide integration point metadata that would be read at |
| 19 | +runtime with ``pkg_resources``. It is now defined as a PyPA interoperability |
| 20 | +specification in order to allow build tools other than setuptools to publish |
| 21 | +``pkg_resources`` compatible entry point metadata, and runtime libraries other |
| 22 | +than ``pkg_resources`` to portably read published entry point metadata |
| 23 | +(potentially with different caching and conflict resolution strategies). |
| 24 | + |
| 25 | +Data model |
| 26 | +========== |
| 27 | + |
| 28 | +Conceptually, an entry point is defined by three required properties: |
| 29 | + |
| 30 | +- The **group** that an entry point belongs to indicates what sort of object it |
| 31 | + provides. For instance, the group ``console_scripts`` is for entry points |
| 32 | + referring to functions which can be used as a command, while |
| 33 | + ``pygments.styles`` is the group for classes defining pygments styles. |
| 34 | + The consumer typically defines the expected interface. To avoid clashes, |
| 35 | + consumers defining a new group should use names starting with a PyPI name |
| 36 | + owned by the consumer project, followed by ``.``. Group names must be one or |
| 37 | + more groups of letters, numbers and underscores, separated by dots (regex |
| 38 | + ``^\w+(\.\w+)*$``). |
| 39 | + |
| 40 | +- The **name** identifies this entry point within its group. The precise meaning |
| 41 | + of this is up to the consumer. For console scripts, the name of the entry point |
| 42 | + is the command that will be used to launch it. Within a distribution, entry |
| 43 | + point names should be unique. If different distributions provide the same |
| 44 | + name, the consumer decides how to handle such conflicts. The name may contain |
| 45 | + any characters except ``=``, but it cannot start or end with any whitespace |
| 46 | + character, or start with ``[``. For new entry points, it is recommended to |
| 47 | + use only letters, numbers, underscores, dashes and dots (regex ``[\w-.]+``). |
| 48 | + |
| 49 | +- The **object reference** points to a Python object. It is either in the form |
| 50 | + ``importable.module``, or ``importable.module:object.attr``. Each of the parts |
| 51 | + delimited by dots and the colon is a valid Python identifier. |
| 52 | + It is intended to be looked up like this:: |
| 53 | + |
| 54 | + import importlib |
| 55 | + modname, qualname_separator, qualname = object_ref.partition(':') |
| 56 | + obj = importlib.import_module(modname) |
| 57 | + if qualname_separator: |
| 58 | + for attr in qualname.split('.'): |
| 59 | + obj = getattr(obj, attr) |
| 60 | + |
| 61 | +.. note:: |
| 62 | + Some tools call this kind of object reference by itself an 'entry point', for |
| 63 | + want of a better term, especially where it points to a function to launch a |
| 64 | + program. |
| 65 | + |
| 66 | +There is also an optional property: the **extras** are a set of strings |
| 67 | +identifying optional features of the distribution providing the entry point. |
| 68 | +If these are specified, the entry point requires the dependencies of those |
| 69 | +'extras'. See the metadata field :ref:`metadata_provides_extra`. |
| 70 | + |
| 71 | +Using extras for an entry point is no longer recommended. Consumers should |
| 72 | +support parsing them from existing distributions, but may then ignore them. |
| 73 | +New publishing tools need not support specifying extras. The functionality of |
| 74 | +handling extras was tied to setuptools' model of managing 'egg' packages, but |
| 75 | +newer tools such as pip and virtualenv use a different model. |
| 76 | + |
| 77 | +File format |
| 78 | +=========== |
| 79 | + |
| 80 | +Entry points are defined in a file called ``entry_points.txt`` in the |
| 81 | +``*.dist-info`` directory of the distribution. This is the directory described |
| 82 | +in :pep:`376` for installed distributions, and in :pep:`427` for wheels. |
| 83 | +The file uses the UTF-8 character encoding. |
| 84 | + |
| 85 | +The file contents are in INI format, as read by Python's :mod:`configparser` |
| 86 | +module. However, configparser treats names as case-insensitive by default, |
| 87 | +whereas entry point names are case sensitive. A case-sensitive config parser |
| 88 | +can be made like this:: |
| 89 | + |
| 90 | + import configparser |
| 91 | + |
| 92 | + class CaseSensitiveConfigParser(configparser.ConfigParser): |
| 93 | + optionxform = staticmethod(str) |
| 94 | + |
| 95 | +The entry points file must always use ``=`` to delimit names from values |
| 96 | +(whereas configparser also allows using ``:``). |
| 97 | + |
| 98 | +The sections of the config file represent entry point groups, the names are |
| 99 | +names, and the values encode both the object reference and the optional extras. |
| 100 | +If extras are used, they are a comma-separated list inside square brackets. |
| 101 | + |
| 102 | +Within a value, readers must accept and ignore spaces (including multiple |
| 103 | +consecutive spaces) before or after the colon, between the object reference and |
| 104 | +the left square bracket, between the extra names and the square brackets and |
| 105 | +colons delimiting them, and after the right square bracket. The syntax for |
| 106 | +extras is formally specified as part of :pep:`508` (as ``extras``). |
| 107 | +For tools writing the file, it is recommended only to insert a space between the |
| 108 | +object reference and the left square bracket. |
| 109 | + |
| 110 | +For example:: |
| 111 | + |
| 112 | + [console_scripts] |
| 113 | + foo = foomod:main |
| 114 | + # One which depends on extras: |
| 115 | + foobar = foomod:main_bar [bar,baz] |
| 116 | + |
| 117 | + # pytest plugins refer to a module, so there is no ':obj' |
| 118 | + [pytest11] |
| 119 | + nbval = nbval.plugin |
| 120 | + |
| 121 | +Use for scripts |
| 122 | +=============== |
| 123 | + |
| 124 | +Two groups of entry points have special significance in packaging: |
| 125 | +``console_scripts`` and ``gui_scripts``. In both groups, the name of the entry |
| 126 | +point should be usable as a command in a system shell after the package is |
| 127 | +installed. The object reference points to a function which will be called with |
| 128 | +no arguments when this command is run. The function may return an integer to be |
| 129 | +used as a process exit code, and returning ``None`` is equivalent to returning |
| 130 | +``0``. |
| 131 | + |
| 132 | +For instance, the entry point ``mycmd = mymod:main`` would create a command |
| 133 | +``mycmd`` launching a script like this:: |
| 134 | + |
| 135 | + import sys |
| 136 | + from mymod import main |
| 137 | + sys.exit(main()) |
| 138 | + |
| 139 | +The difference between ``console_scripts`` and ``gui_scripts`` only affects |
| 140 | +Windows systems. ``console_scripts`` are wrapped in a console executable, |
| 141 | +so they are attached to a console and can use ``sys.stdin``, ``sys.stdout`` and |
| 142 | +``sys.stderr`` for input and output. ``gui_scripts`` are wrapped in a GUI |
| 143 | +executable, so they can be started without a console, but cannot use standard |
| 144 | +streams unless application code redirects them. Other platforms do not have the |
| 145 | +same distinction. |
| 146 | + |
| 147 | +Install tools are expected to set up wrappers for both ``console_scripts`` and |
| 148 | +``gui_scripts`` in the scripts directory of the install scheme. They are not |
| 149 | +responsible for putting this directory in the ``PATH`` environment variable |
| 150 | +which defines where command-line tools are found. |
| 151 | + |
| 152 | +As files are created from the names, and some filesystems are case-insensitive, |
| 153 | +packages should avoid using names in these groups which differ only in case. |
| 154 | +The behaviour of install tools when names differ only in case is undefined. |
0 commit comments