How to fix 'InvalidDistribution: Metadata is missing required fields' Error when uploading to third-party PyPI Registry

---
date: Jun 10, 2025
tags:
- Python
- Forgejo
- PyPI
language: English
---

The issue

I am trying to build one of my project using Python’s build module and then upload it to my own Forgejo Git instance’s PyPI Package Registry using twine. Although the build suceeded, but it always fails to upload with the following error:

1
2
3
InvalidDistribution: Metadata is missing required fields: Name, Version.

Make sure the distribution includes the files where those fields are specified, and is using a supported Metadata-Version: 1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 2.3.

After some research, I finally realized exactly what happened after breaking the CI several times, which made me feel really stupid.

The cause

When building a Python package using build, it will try to use the proper setuptools to build the Python package. However, if you do not specify any limitation to the version of setuptools, it will always use the latest version. Now, with the latest version of setuptools, the default Metadata-Version has switched to 2.4. This is fine for the Official PyPI website, but for third-party PyPI registries, like in this case my self-hosted Forgejo instance’s PyPI Registry, they might not be supported yet.

The solution

To solve this, it is actually really simple. However, first make sure to switch to the new(?) pyproject.toml. Then, you can limit the generated version. Include the following in the pyproject.toml to pin the version of setuptools:

1
2
3
4
5
6
7
...

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

...

With this done, you can now properly upload the packages to PyPI Registries that only support Metadata-Version versions older than 2.4.