Skip to content

Python 3.12, pyproject.toml, distutils and a new build system #52

Closed
@oscarbenjamin

Description

@oscarbenjamin

Currently python-flint builds using a setup.py and also uses distutils and numpy.distutils (see #4). As of Python 3.12 the stdlib distutils module is removed and also numpy is removing its own distutils module. This means that python-flint needs to move to a new build system somehow. Also setup.py should be removed and replaced with pyproject.toml and it probably makes sense to do that at the same time.

There are a few options for build system:

  1. Try to use setuptools instead of distutils.
  2. Switch to meson and meson-python.
  3. Use cmake

Various Python projects seem to be choosing one of these three options. For pure Python projects there are other options like poetry, hatch, etc. Some of those can handle extension module building but I am not sure how well any of them works. The people who seem to know most about packaging extension modules seem to suggest either meson or cmake. In particular meson is being used by SciPy now and will be for NumPy soon (if not already).

To me switching to meson seems like the best long term option. I made a start on that here:
mesonbuild/meson-python#410
I discussed lots of things there like editable installs etc that make things a bit more complicated but the most important thing is just to have a build system that can produce wheels either for local install or upload to PyPI. Restricting to just producing wheels means that it would probably not be too hard to get something working with meson but the work I started here just needs to be carried through:
https://github.com/oscarbenjamin/mesontest

I don't know much about how using cmake would work here or what the potential upsides/downsides are.

Potentially the quickest fix for the removal of distutils is just to replace distutils with setuptools. It is probably not hard for someone to test that:

  • Replace distutils with setuptools in setup.py
  • Fix any immediate problems until you have a local build
  • Push up to a PR in this repo and see if CI can build for all platforms and architectures.

One thing that might be a problem with setuptools is the use of MinGW compilers on Windows but this is tested in CI so we should know if it is working or not. My understanding is that numpy.distutils has special support for MinGW that is not in distutils or setuptools. If that means that the Windows build fails with setuptools then it probably means that python-flint just can't use setuptools.

Switching somehow to MSVC rather than MinGW would be good but I have already explored the options for doing that with GMP and I think we just have to give up on that. What might be possible is using MinGW to make a fat build of GMP and then using MSVC to compile everything else or perhaps just for the python-tools extension module. I don't know enough about MSVC to investigate that option so someone else would need to explore that. If we could get MSVC to pick up the MinGW compiled GMP then that might make it more possible to use setuptools or probably makes things easier in some sense for any of the build system choices.

Along with the new build system we should replace setup.py with pyproject.toml which is how the build system should be configured for the project in modern Python packaging. Other things can also be moved into pyproject.toml:

  • Build-time dependencies (Missing dependencies #24) - I don't see how to add these in setup.py.
  • All of the cibuildwheel configuration variables that are currently in the CI config and some of the scripts in bin/.

It possibly makes sense to create a pyproject.toml before attempting to use a new build system because there needs to be a pyproject.toml in order for any standardised build system to be used and for cibuildwheel to work without setup.py.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions