Expose CLI Entry Points and Package Resources
Core idea
A [project.scripts] mapping declares console script names and module:function targets. An installer creates the launcher. Data files must also be included by the selected backend; use importlib.resources to read installed resources without assuming a physical path. The metadata declaration and data inclusion are separate.
import tomllibp = tomllib.loads("""[project]name = "example-parcel"version = "0.1.0"[project.scripts]parcel = "parcel.cli:main"""")print(p['project']['scripts']['parcel'])The script name maps to a callable, not a shell command string.
Expected output:
parcel.cli:mainRead a trusted installed resource
from importlib.resources import filesp = files('email').joinpath('__init__.py')print(p.is_file())print(bool(p.read_bytes()))The standard package demonstrates Traversable access; your package must include its own data at build time.
Expected output:
TrueTrueCommon mistake
A script declaration does not install the command by itself. Do not depend on current working directory for package data. This example assumes the normal CPython standard library.
Practice
Add a main function and a small UTF-8 resource to a trusted local package. Configure backend data inclusion, build and install in a disposable environment, then verify the launcher and resource.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Entry points and package data
Check your understanding of entry points and package data.
What is printed?
import tomllibprint(tomllib.loads("[project.scripts]\nhi=\"demo:main\"")["project"]["scripts"]["hi"])What is printed?
from importlib.resources import filesprint(files("email").joinpath("__init__.py").is_file())Who creates a console launcher?
What must be checked after building?