Question

Unable to upload PIP package to Google Artifact Registry using twine, err: KeyError: 'license'

I am building a Python PIP package to be stored on Google Artifact Registry.

The pyproject.toml is as below:

[build-system]
requires = ["setuptools>=68.0.0,<69.0.0", "setuptools-scm>=8.0.1,<8.1.0"]
build-backend = "setuptools.build_meta"

[project]
name = "my-sdk"
version = "0.1.3"
license = {text = "Some Solution"}
readme = "README.md"
requires-python = ">=3.8,<3.11"
dependencies = [
    "google-cloud-storage>=2.6.0"
]
[project.optional-dependencies]
ci = [
    "flake8==6.0.0",
    "Flake8-pyproject==1.2.3",
    "pytest-cov==4.1.0",
    "pytest==7.2.2"
]

[tool.setuptools.packages.find]
include = ["*"]
namespaces = false

[tool.pytest]
norecursedirs = [".git", ".tox"," venv*"]
addopts = "-rsxX -q -p no:warnings"
python_files = ["*_test.py", "test_*.py"]
log_cli = true
log_cli_level = "INFO"

[tool.flake8]
max-line-length = 100
select = ["E", "W", "F", "C", "B"]
ignore = ["E203", "W503", "E731"]
per-file-ignores = ["__init__.py: F401", "test_*.py: D103"]

Sequence of commands for the package:

python -m pip install \
            keyring==24.3.0 \
            keyrings.google-artifactregistry-auth==1.1.2 \
            build==1.0.3 \
            twine==4.0.2
python -m build
python -m twine upload \
            --repository-url https://<hidden>.pkg.dev/my-project/my-artifacts \
            dist/*

Only the last step fails, i.e. twine upload, and the error message is below:

Traceback (most recent call last):
  File "/miniconda/envs/pip_env/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/miniconda/envs/pip_env/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/miniconda/envs/pip_env/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/miniconda/envs/pip_env/lib/python3.10/site-packages/twine/__init__.py", line 43, in <module>
    __license__ = metadata["license"]
  File "/miniconda/envs/pip_env/lib/python3.10/site-packages/importlib_metadata/_adapters.py", line 54, in __getitem__
    raise KeyError(item)
KeyError: 'license'

I have tried different types of license in pyproject.toml, but getting the same error everytime. Any one else faced this challenge?

 3  554  3
1 Jan 1970

Solution

 1

After looking through the twine issues extensively, I couldn't find any relevant answer. However, I did see that in pypi.org, 5.1.0 version was yanked due to a similar error: KeyError. Thus I decided to upgrade to 5.1.1, and then twine upload worked out.

python -m pip install \
            keyring==24.3.0 \
            keyrings.google-artifactregistry-auth==1.1.2 \
            build==1.0.3 \
            twine==5.1.1

As the message below mentions, 5.1.1 addresses the release of importlib-metadata==8.0.0.

https://github.com/pypa/twine/issues/1125#issuecomment-2191941192

2024-07-02
Chinmaya Biswal

Solution

 1

Somewhere in importlib-metadata 7.x a deprecation warning was added for DeprecationWarning: Implicit None on return values is deprecated and will raise KeyErrors. importlib-metadata v8 was released a few days ago, which now raises the key error. Try adding a restriction for importlib-metadata<8

[project.optional-dependencies]
ci = [
    "flake8==6.0.0",
    "Flake8-pyproject==1.2.3",
    "pytest-cov==4.1.0",
    "pytest==7.2.2",
    "importlib-metadata<8.0.0"
]

You'll need to manage this dependence until it gets addressed in twine. Twine offers no upper limit for importlib-metadata https://github.com/pypa/twine/blob/2e0bd6f2376a9549b47d42db79244bac6ed047c8/pyproject.toml#L38

2024-07-02
M.M

Solution

 0

This resolved the issue for me:

pip3 install importlib-metadata==7.1.0 twine==5.1.1
2024-07-18
maslick