How to specify setuptools entrypoints in a pyproject.toml
Asked Answered
O

1

9

I have a setup.py like this:

#!/usr/bin/env python

from setuptools import setup, find_packages

setup(
    name="myproject",
    package_dir={"": "src"},
    packages=find_packages("src"),
    entry_points={
        "console_scripts": [
            "my-script = myproject.myscript:entrypoint",
        ],
    },
)

How can I write that entry_points configuration in pyproject.toml using setuptools?

I'm guessing something like this, going on setuptools' pyproject.toml docs, which says I need to use "INI format" following the docs that references for entry-points but it doesn't seem to give an example, and my guess at how to combine the setuptools syntax with the pyproject.toml syntax is wrong (I get a traceback from pip install -e . that reports pip._vendor.tomli.TOMLDecodeError: Invalid value, pointing at the entry-points line in pyproject.toml):

[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[metadata]
name = "myproject"

[tool.setuptools]
package-dir = {"" = "src"}

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.dynamic]
entry-points =
    my-script = myproject.myscript:entrypoint

Note I have a stub setup.py alongside that pyproject.toml, like this (which I read I need to support pip install -e . i.e. "editable installation"):

from setuptools import setup

if __name__ == "__main__":
    setup()
Olivaolivaceous answered 11/2, 2023 at 11:17 Comment(0)
E
14

See the specification and this guide:

[project.scripts]
spam-cli = "spam:main_cli"

[project.gui-scripts]
spam-gui = "spam:main_gui"

[project.entry-points."spam.magical"]
tomatoes = "spam:main_tomatoes"

Also nowadays (and since the adoption of PEP-660), you should not need the (stub) setup.py script at all for editable installation. Simply use python -m pip install --editable ..

Ecosystem answered 11/2, 2023 at 11:28 Comment(6)
Thanks! I'm quite surprised by that spec with all the [project] sections -- is the link in this comment (below) out of date less than a year later? For example, it says to put the project name in a [metadata] section, not in a [prooject] section godatadriven.com/blog/… Specs are great for implementors but to get a project going quickly an example is a lot faster Edit: oh, in that blog I now see they're using pyproject.toml AND setup.cfg :-|Olivaolivaceous
This blog is out of date, yes. It is still correct, you can use setup.cfg, without problem, but nowadays if you start a new project it is probably better to go directly for pyproject.toml instead.Ecosystem
Also for editable installation, there is no need for setup.py anymore.Ecosystem
Note that in order to use pyproject.toml without setup.py, you need to require a recent-enough version of setuptools, like so: requires = ["setuptools>=61.2", ...] Also note that his doesn't affect your dev environment (unless --no-build-isolation is passed to pip)Salyers
Link is dead and answer is possibly outdated again? Is this the current spec? packaging.python.org/en/latest/specifications/entry-pointsFacetiae
@BradyGilg Thanks for the notification. I updated the answer with newest link to specification and additional link to guide.Ecosystem

© 2022 - 2025 — McMap. All rights reserved.