pyang icon indicating copy to clipboard operation
pyang copied to clipboard

pyang cannot run in Python 3.12 (pkg_resources)

Open JennToo opened this issue 1 year ago • 2 comments

Traceback (most recent call last):
  File "/tmp/build/.porg/venvs/3.12-d/bin/pyang", line 5, in <module>
    from pyang.scripts.pyang_tool import run
  File "/tmp/build/.porg/venvs/3.12-d/lib/python3.12/site-packages/pyang/scripts/pyang_tool.py", line 12, in <module>
    from pyang import plugin
  File "/tmp/build/.porg/venvs/3.12-d/lib/python3.12/site-packages/pyang/plugin.py", line 5, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

pkg_resources has been deprecated for a while now and was finally removed in 3.12 it seems.

JennToo avatar Jun 20 '24 13:06 JennToo

I ran into the same problem when my default venv was created with setuptools version 69. When I upgraded it via pip install --upgrade setuptools to 72.2.0 it worked again.

991jo avatar Aug 14 '24 06:08 991jo

pkg_resources can be replaced by importlib.resources which is available since python 3.7 (the oldest version supported by pyang)

Note: I ran into this using python 3.11 but installing pyang with uv which does not install setuptools by default:

$ uvx  pyang --version
Traceback (most recent call last):
...
ModuleNotFoundError: No module named 'pkg_resources'

A workaround that works for me is:

$ uvx --with setuptools pyang --version
pyang 2.6.1

IMHO this is a bug in pyang because it uses pkg_resources without declaring setuptools as a dependency.

cpascual avatar Oct 09 '24 17:10 cpascual

It looks like this was only partially fixed (i.e. removed dependency on setuptools for python3.12+). For 3.11 and lower the dependency is still implicit, causing issues like:

   > uv tool run --from git+https://github.com/mbj4668/pyang -p 3.11 pyang --version
▒ Resolving dependencies...
▒▒Installed 2 packages in 15ms
Traceback (most recent call last):
  File "/users/legaul/.cache/uv/archive-v0/svidWkWP2eg3WsOi2WiDA/bin/pyang", line 6, in <module>
    from pyang.scripts.pyang_tool import run
  File "/nobackup/legaul/.cache/uv/archive-v0/svidWkWP2eg3WsOi2WiDA/lib/python3.11/site-packages/pyang/scripts/pyang_tool.py", line 12, in <module>
    from pyang import plugin
  File "/nobackup/legaul/.cache/uv/archive-v0/svidWkWP2eg3WsOi2WiDA/lib/python3.11/site-packages/pyang/plugin.py", line 8, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

https://docs.python.org/3/library/importlib.metadata.html was added in 3.8 - why not use it for all versions?

LewisGaul avatar Feb 05 '25 16:02 LewisGaul

It seems it doesn't work the same put of the box in 3.8 and 3.9. But we can use it from 3.10.

mbj4668 avatar Feb 06 '25 07:02 mbj4668

The dependency on setuptools isn't really the problem, it's that it's not listed in the project's dependencies. So could it be added (preferably only for those older Python version where it's needed)?

LewisGaul avatar Feb 06 '25 09:02 LewisGaul

> pip install --upgrade setuptools

This fixed it for me. Running Python 3.13.1

jillesca avatar Apr 21 '25 12:04 jillesca