tox-dev / pipdeptree

A command line utility to display dependency tree of the installed Python packages
https://pypi.python.org/pypi/pipdeptree
MIT License
2.73k stars 147 forks source link

Broken in python 3.10 #350

Closed soerenwolfers closed 2 months ago

soerenwolfers commented 2 months ago

What pipdeptree version are you using?

2.17.0

Are you running pipdeptree in a virtual environment?

Describe the problem

Getting

ValueError: Couldn't determine installed packags: Traceback (most recent call last):
...PackageDAG.from_pkgs([x for x in pkgs if PACKAGE_NAME == x.key)
  File "/home/admin/.local/lib/python3.10/site-packages/pipdeptree/_models/dag.py", line 42, in from_pkgs
    dist_pkgs = [DistPackage(p) for p in pkgs]
  File "/home/admin/.local/lib/python3.10/site-packages/pipdeptree/_models/dag.py", line 42, in <listcomp>
    dist_pkgs = [DistPackage(p) for p in pkgs]
  File "/home/admin/.local/lib/python3.10/site-packages/pipdeptree/_models/package.py", line 95, in __init__
    super().__init__(obj.metadata["Name"])
  File "/home/admin/.local/lib/python3.10/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2903, in __getattr__
    return getattr(self._provider, attr)
AttributeError: 'PathMetadata' object has no attribute 'metadata'

python3.10 importlib_metadata==7.1.0 pipdeptree=2.17.0

https://github.com/tox-dev/pipdeptree/blame/main/src/pipdeptree/_models/package.py#L103

kemzeb commented 2 months ago

It looks like this is because you are passing a list of pip._vendor.pkg_resources.DistInfoDistribution when we now expect importlib.metadata.Distribution.

pip._vendor.pkg_resources.DistInfoDistribution is a sublcass of pip._vendor.pkg_resources.Distribution, and when we try to perform obj.metadata['Name'], the metadata attribute fails to be looked up, and since the class implements __getattr__(), it uses it (see implementation here). It delegates attribute lookup to self._provider, but since metadata doesn't exist here either we will see the failure.

Looking at the stack trace further, it looks like you are using our API. Sadly, this is internal API and therefore may change in future releases.

kemzeb commented 2 months ago

I don't recommend you continue using the API, but if you really need it I think I see three options:

soerenwolfers commented 2 months ago

Thanks for the quick response. Will probably change tooling, I really do need a stable API, not a command line tool