same: Using Poetry to Change Python Versions and Create New Virtual Environment

Using Entry Points in Poetry#

If you need to use entry points in a Poetry project, you can use a feature in pyproject.toml for the same behavior as in setuptools. This feature is the heading [tool.poetry.scripts]. Under this, you create the linkage between a command and a function in your script in the form of cmd = "package.module.path:function", giving the fully qualified location of the function using the same dot notation as you would with an import statement.

Example with Click#

Given the script yourscript.py:

import click

@click.command()
def cli():
    """Example script."""
    click.echo('Hello World!')

In Poetry, you’ll add this to your pyproject.toml

[tool.poetry.scripts]
yourscript_cmd = "yourscript:cli" # command_name = "package.module.path:function"

The setuptools equivalent would be:

setup(
    ...,
    entry_points={
        "console_scripts": [
            "yourscript_cmd = yourscript:cli", # 1st yourscript is the callable name, then the rest defines import path, and after colon is the Click command
        ]
    }
)

To call this example, you would call yourscript_cmd in your favorite terminal emulator.

Sources#


  1. This documentation claims that plugins are the equivalent to setuptools’ entry_points, but that does not work as expected. ↩︎