Page MenuHomePhabricator

Pywikibot importlib.metadata.entry_points error in Python 3.9
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

  • Debian 11 with Python 3.9.2
  • Create virtual environment in target folder: python3 -m venv venv
  • Activate virtual environment: source ./venv/bin/activate
  • Install requirements: pip install -r requirements.txt or just pip install py-postgresql pywikibot
# requirements.txt
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10
mwparserfromhell==0.6.6
packaging==24.2
pkg_resources==0.0.0
py-postgresql==1.3.0
pywikibot==9.6.0
requests==2.32.3
urllib3==2.2.3
  • Input command: pwb generate_user_files

What happens?:

Error raises (I changed part of target directory path to "..."):

Traceback (most recent call last):
  File "/home/.../venv/bin/pwb", line 8, in <module>
    sys.exit(run())
  File "/home/.../venv/lib/python3.9/site-packages/pywikibot/scripts/wrapper.py", line 539, in run
    if not execute():
  File "/home/.../venv/lib/python3.9/site-packages/pywikibot/scripts/wrapper.py", line 479, in execute
    filename = find_filename(filename)
  File "/home/.../venv/lib/python3.9/site-packages/pywikibot/scripts/wrapper.py", line 425, in find_filename
    for ep in entry_points(name='scriptspath', group='pywikibot'):
TypeError: entry_points() got an unexpected keyword argument 'name'
CRITICAL: Exiting due to uncaught exception TypeError: entry_points() got an unexpected keyword argument 'name'

We can see that pywikibot wrapper.py module uses only "new-style" entry_points() function from importlib.metadata.

What should have happened instead?:

Steps for generating user-config.py file (it works for Python 3.12)

Software version

  • Debian GNU/Linux 11 (bullseye)
  • Python 3.9.2
  • pywikibot 9.6.0

Event Timeline

Can fix it if insert in wrapper.py something like this (version info):

# ... <before line 419> ...
    if site_package:  # search for entry points
        import importlib
        from importlib.metadata import entry_points

        from pywikibot.i18n import set_messages_package

        if sys.version_info < (3, 10):
            entry_points_items = [ep for ep in entry_points().get("pywikibot", []) if ep.name == "scriptspath"]
        else:
            entry_points_items = entry_points(name='scriptspath', group='pywikibot')

        for ep in entry_points_items:
            path = ep.load()
            found = test_paths([''], path)
# ...

This still needs to be verified

Change #1101208 had a related patch set uploaded (by JJMC89; author: JJMC89):

[pywikibot/core@master] Fix use of importlib.metadata.entry_points in python 3.9

https://gerrit.wikimedia.org/r/1101208

Change #1101208 merged by jenkins-bot:

[pywikibot/core@master] Fix use of importlib.metadata.entry_points in python 3.9

https://gerrit.wikimedia.org/r/1101208

For those with the current release from PyPi, which will be affected by this by with older Pythons, you can apply the Gerrit patch immediately in this way:

# using the example of pywikibot installed with pipx
cd ~.local/pipx/venvs/pywikibot/lib/python3.8/site-packages  # replace 3.8 w/ appropriate Python version

curl -LOJ 'https://gerrit.wikimedia.org/r/changes/pywikibot%2Fcore~1101208/revisions/1/patch?zip&path=pywikibot%2Fscripts%2Fwrapper.py'

# see also: funzip(1)
unzip 08c28cb.diff.zip
patch -p1 < 08c28cb.diff