Bad metadata: TypeError: expected string or bytes-like object, got 'NoneType'
See original GitHub issueDescription
Since pip 22, several Fedora Rawhide users reported the following problem for many (all?) pip commands:
File "/usr/lib/python3.11/site-packages/pip/_internal/metadata/base.py", line 604, in
iter_all_distributions
for dist in self._iter_distributions():
File "/usr/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 167, in _iter_distributions
yield from finder.find(location)
File "/usr/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 71, in find
for dist, info_location in self._find_impl(location):
File "/usr/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 59, in _find_impl
normalized_name = canonicalize_name(get_dist_name(dist))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/pip/_vendor/packaging/utils.py", line 34, in canonicalize_name
value = _canonicalize_regex.sub("-", name).lower()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'
I could not reproduce at first, but after investigation, @encukou figured out that this works as a repducer:
# mkdir /usr/lib/python3.11/site-packages/bad.dist-info
# python3 -m pip list
...
TypeError: expected string or bytes-like object, got 'NoneType'
This is not Fedora-pip specific, I can reproduce it in the official python:3.11.0b5 container (see the reproducer below).
This happens with pip-22.2.2, pip-22.2.
This does not happen with pip-21.3.1.
This does not seem to happen with Python 3.10.
I suspect this is related to the switch to importlib metadata.
Expected behavior
This is how it worked before:
# mkdir /usr/local/lib/python3.11/site-packages/bad.dist-info
# python3 -m pip list
Package Version
---------- -------
pip 21.3.1
setuptools 63.2.0
wheel 0.37.1
pip version
22.2.2
Python version
3.11.0b5
OS
Linux
How to Reproduce
$ podman run --rm -ti python:3.11.0b5 /bin/bash
# mkdir /usr/local/lib/python3.11/site-packages/bad.dist-info
# python3 -m pip list
Output
ERROR: Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/base_command.py", line 167, in exc_logging_wrapper
status = run_func(*args)
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/commands/list.py", line 164, in run
packages: "_ProcessedDists" = [
^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/commands/list.py", line 164, in <listcomp>
packages: "_ProcessedDists" = [
^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/base.py", line 646, in <genexpr>
return (d for d in it if d.canonical_name not in skip)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/base.py", line 594, in iter_all_distributions
for dist in self._iter_distributions():
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 167, in _iter_distributions
yield from finder.find(location)
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 71, in find
for dist, info_location in self._find_impl(location):
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 59, in _find_impl
normalized_name = canonicalize_name(get_dist_name(dist))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_vendor/packaging/utils.py", line 34, in canonicalize_name
value = _canonicalize_regex.sub("-", name).lower()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/usr/local/lib/python3.11/site-packages/pip/__main__.py", line 31, in <module>
sys.exit(_main())
^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/main.py", line 70, in main
return command.main(cmd_args)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/base_command.py", line 101, in main
return self._main(args)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/base_command.py", line 223, in _main
self.handle_pip_version_check(options)
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/req_command.py", line 179, in handle_pip_version_check
session = self._build_session(
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/cli/req_command.py", line 125, in _build_session
session = PipSession(
^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/network/session.py", line 343, in __init__
self.headers["User-Agent"] = user_agent()
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/network/session.py", line 175, in user_agent
setuptools_dist = get_default_environment().get_distribution("setuptools")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 180, in get_distribution
return next(matches, None)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 175, in <genexpr>
matches = (
^
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/base.py", line 594, in iter_all_distributions
for dist in self._iter_distributions():
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 167, in _iter_distributions
yield from finder.find(location)
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 71, in find
for dist, info_location in self._find_impl(location):
File "/usr/local/lib/python3.11/site-packages/pip/_internal/metadata/importlib/_envs.py", line 59, in _find_impl
normalized_name = canonicalize_name(get_dist_name(dist))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/pip/_vendor/packaging/utils.py", line 34, in canonicalize_name
value = _canonicalize_regex.sub("-", name).lower()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'
Code of Conduct
- I agree to follow the PSF Code of Conduct.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
How to Fix: Typeerror: expected string or bytes-like object
This tutorial explains how to fix the following error in Python: How to Fix: Typeerror: expected string or bytes-like object.
Read more >TypeError expected string or bytes-like object - STechies
It might be expecting a string or byte like object but as it has received something else, it raises an error. The way...
Read more >re.sub erroring with "Expected string or bytes-like object"
The simplest solution is to apply Python str function to the column you are trying to loop through. If you are using pandas...
Read more >TypeError: expected string or bytes-like object in Python
To solve the error, call the read() method on the file object to get a string of the file's contents and pass the...
Read more >Problems in new parsers (#484) · Issues - MPCDF Gitlab
Quantity(data[0], 'eV') TypeError: 'NoneType' object is not subscriptable ... (expected 2, got 0) During handling of the above exception, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ignoring bad metadata with a warning is a good solution to me.
Do we need to worry how the bad metadata get in there in the first place? Or are those generated by Fedora so we don’t need to worry?
BTW, the warning is quite noisy: