Skip to content

Commit 9abdad2

Browse files
committed
Explain how package and installed CLI entry points share a callable
1 parent 5b31c28 commit 9abdad2

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎source/guides/creating-command-line-tools.rst‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Creating the package
1414
First of all, create a source tree for the :term:`project <Project>`. For the sake of an example, we'll
1515
build a simple tool outputting a greeting (a string) for a person based on arguments given on the command-line.
1616

17-
.. todo:: Advise on the optimal structure of a Python package in another guide or discussion and link to it here.
18-
1917
This project will adhere to :ref:`src-layout <src-layout-vs-flat-layout>` and in the end be alike this file tree,
2018
with the top-level folder and package name ``greetings``:
2119

@@ -95,6 +93,16 @@ so initialize the command-line interface here:
9593
from greetings.cli import app
9694
app()
9795
96+
In this layout, importing ``greetings`` runs :file:`__init__.py`, which can
97+
stay empty. Running ``python -m greetings`` then runs :file:`__main__.py`.
98+
Keep that entry point small and put the reusable CLI callable in an importable
99+
module such as :file:`cli.py`. Both :file:`__main__.py` and the installed
100+
script declared in ``[project.scripts]`` can call the same object. Code that
101+
needs that callable can import it from ``greetings.cli``.
102+
See the `Python documentation for __main__`_ for more details.
103+
104+
.. _Python documentation for __main__: https://docs.python.org/3/library/__main__.html
105+
98106
.. note::
99107

100108
In order to enable calling the command-line interface directly from the :term:`source tree <Project Source Tree>`,

0 commit comments

Comments
 (0)