Is there a simple way to convert setup.py to pyproject.toml
Asked Answered
J

3

30

We have multiple python projects, and are considering converting them to use pyproject.toml instead of setup.py.

Is there a simple way to automate this?

Joly answered 1/7, 2022 at 16:13 Comment(1)
I guess something like run python setup.py egg_info, figure out in which file the metadata is written, parse it and write it back to a pyproject.toml file. Should be relatively straightforward. And should be quite reliable unless the setup.py files do things they should not be doing. -- I thought I had seen a tool that does this but can not find it anymore, so maybe I hallucinated.Plantar
J
22

While dealing with some pyproject.toml issues, I encountered this project:

https://pypi.org/project/ini2toml/

The projects description:

This project is experimental and under active development Issue reports and contributions are very welcome. The original purpose of this project is to help migrating setup.cfg files to PEP 621, but by extension it can also be used to convert any compatible .ini/.cfg file to .toml.

While this only helps to turn .cfg/.ini files to PEP 621 .toml, there is another project that turn setup.py files into cfg files.

https://github.com/gvalkov/setuptools-py2cfg

This script helps convert existing setup.py files to setup.cfg in the format expected by setuptools.

Combining these two processes by writing a script you could potentially come up with an automated way to transform the files. I have not tried this as of yet but would be interested if you could make this idea work :)

Jamin answered 23/9, 2022 at 16:16 Comment(1)
This didn't work for me, I'm still getting build-related errors after generating the tomlBentley
S
13

pdm supports importing metadata from various existing and older metadata files, including setup.py.

You can either:

  1. run pdm init on your project root (with the old metadata files) and follow the instructions, or
  2. run pdm import setup.py explicitly.

See Import project metadata from existing project files for more details.

Shipe answered 4/2, 2023 at 13:1 Comment(3)
just for records, it automatically updates the pyproject.toml fileLyly
but also that it didn't add even a single new line about [build-system], only added the ones in the setup() scope from the setup.py fileLyly
rly helpful. It's a significant improvement to have a pyproject.toml available for pip-tools' pip-compile, juxtaposed to the setup.py files for some older Python projects (e.g pyQode). The pdm import cmd was just so simple - will take another look at using pdm. thx!Mesencephalon
L
3

You can also do the migration using Hatch tool

Navigate to the directory where you setup.py is located and run

hatch new --init

and it will automatically migrate to the pyproject.toml configuration for you

Leopoldoleor answered 19/2 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.