Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

As someone who actively contributes to Python packages using the setuptools cli interface, this is jarring. I had no idea. I guess I'll move my build process to some other tools.

I started contributing to Python about a year ago. At that time, all of the results when you google "how to build a python package" refer to the setuptools CLI with setup.py. I put it in my workflow and bam, I've been using it ever since without incident.



You don't need to completely change if you don't want to. setuptools will most likely continue supporting the old way for a long time since there is so much Python code out there that will probably never update to the new standards.

Also, it takes only minimal changes to bring a setuptools-based project into the present. You just need to add a `pyproject.toml` file to your project with the following:

    [build-system]
    requires = ["setuptools >= 40.9.0", "wheel"]
    build-backend = "setuptools.build_meta"
Then when it's time to distribute the project, instead of `setup.py sdist` and `setup.py bdist_wheel`, you can just use the official build[1] tool like so:

    pyproject-build --sdist --wheel
And that's all there is to it. Once that's in place and if you're feeling frisky, you can try taking advantage of new setuptools features that can allow you to eliminate `setup.py` entirely and specify everything in `setup.cfg`[2] so that all your project's information is static and no code needs to be run when installing it, assuming your project doesn't contain any compiled code.

As and added benefit, by putting pyproject.toml in your projects now, if you wanted to switch over to another tool like poetry or flit later on, the act of packaging the project doesn't have to change as long as you update the pyproject.toml file with the new build system. The same `pyproject-build` command will work for all three systems.

[1] https://pypi.org/project/build/

[2] https://setuptools.pypa.io/en/latest/userguide/declarative_c...


Note that with the recent versions of `build` it's recommended not to pass `--sdist --wheel`. Just do:

    python -m build
With this invocation it'll produce an sdist, and then it'll build a wheel from that tarball, not from your Git checkout which is much closer to what pip does in the wild. When building both artifacts from a Git checkout, it's possible to mess up packaging making it impossible to produce a wheel out of an sdist (which is necessary in many places, including installs from tarballs that pip tries to build wheels from and downstream packaging in different OS ecosystems).


Sadly there's no easy way to mark all those Google searches out of date... One of the big goals of this project is to spread the knowledge though, so you reading it means success.


I find myself frequently using the date filter in Google when searching programming topics. Especially if if I'm looking up an error message, almost anything older than a year is noise.


Save this tutorial to your bookmarks, then: https://packaging.python.org/tutorials/packaging-projects/#c...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: