Define a Python Library Target

Problem

You need to turn your Python project into a library target that other libraries in your Pants workspace can use as a dependency.

If you need to create an executable Python binary target instead, see Specify a Python Executable (PEX).

Solution

Define a python_library target definition that designates the library's source files, dependencies, and more.

Discussion

A python_library target definition should specify the following:

  • A name for the target
  • Either a single source Python file or a list of sources
  • A list of dependencies (optional)

Here is an example target definition:

python_library(
  name='my-python-lib',
  sources=['*.py'],
  dependencies=[
    'src/python/myproject/server:server-lib',
    'src/python/myproject/client:client-lib',
    'src/static/json:config',
  ],
)

Now, another library or binary can depend on the target you created:

dependencies=[
  'src/python/myproject/example:my-python-lib',
]

See Also

Generated by publish_docs from dist/markdown/html/src/docs/common_tasks/python_library.html 2022-12-03T01:09:00.303355