pyang cannot run in Python 3.12 (pkg_resources)
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.
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.
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.
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?
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.
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)?