Skip to content

DOC: add some documentation on how to use entry points #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions docs/tutorials/entrypoints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,96 @@
Entry points
************


.. todo::

- Mention what is the purpose of entry points
- Give an example of a console entry point
- Give an example of a custom entry point
- Mention pluggy maybe for an example use-case?

Introduction
============

Entry points provide a mechanism to advertise components of an installed
distribution to other code or users. Most notably, the ``console_scripts``
entry points will create a command line wrapper.

See the `PyPA documentation on entry points <https://packaging.python.org/en/latest/specifications/entry-points/>`_.

Using entry points with meson-python
====================================

Entry points are defined in the ``pyproject.toml`` `metadata specification
<https://packaging.python.org/en/latest/specifications/declaring-project-metadata/#entry-points>`_.
No further configuration is required when using ``meson-python``.

Console entry point
-------------------

To show the usage of console entry points we build a simple
python script:

.. code-block:: python
:caption: src/simpleapp/console.py

"""
Simple test application.

Usage:
simpleapp --help
simpleapp doc
simpleapp run [<file>] [options]

Options:
-h --help display this help message
--workdir-path=<workdir-path> directory in which to run [default: none]

"""
import docopt



def main():
args = docopt.docopt(__doc__)
# Just print the arguments for now.
print(args)

if __name__ == "__main__":
main()

This script will be part of a larger python package (called ``simpleapp``).
The meson build file is very simple and only installs the python sources.

.. code-block:: meson
:caption: meson.build

project('simpleapp', version:'0.0.1')


pymod = import('python')
python = pymod.find_installation('python3')
pydep = python.dependency()

python.install_sources(
'src/simpleapp/__init__.py', 'src/simpleapp/console.py'
, pure: true
, subdir: 'simpleapp'
)

The entry points are then specified in the ``pyproject.toml`` metadata specification.


.. code-block:: toml
:caption: pyproject.toml

[project]
name = "simpleapp"
description = "simple app"
requires-python = ">=3.6"
dependencies = ["docopt"]
version = "0.0.1"

[project.scripts]
simpleapp = "simpleapp.console:main"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding two project.entry-points.NAME entries, that will be instructive. I suggest using the actual ones from numpy (pyinstaller40 and array_api), because those are real-world things. The actual implementation can be a one-liner, something like:

print("You discovered the PyInstaller plugin for simpleapp")

[build-system]
requires = ["meson", "toml", "ninja", "meson-python"]
build-backend = 'mesonpy'