Page MenuHomePhabricator

Suggest installing requests_oauthlib instead of throwing NoUsernameError
Open, LowPublic

Description

When using OAuth credentials in pywikibot, when requests_oauthlib package is not installed, the error show to the used is not helpfull at all.

tools.framabot@shell:~$ ~/venv3.11/bin/python /data/project/shared/pywikibot/core/pwb.py  login
fatal: detected dubious ownership in repository at '/data/project/pywikibot/public_html/core'
To add an exception for this directory, call:

	git config --global --add safe.directory /data/project/pywikibot/public_html/core
ERROR: OAuth authentication not supported: No module named 'requests_oauthlib'
Traceback (most recent call last):
  File "/data/project/shared/pywikibot/core/pwb.py", line 39, in <module>
    sys.exit(main())
             ^^^^^^
  File "/data/project/shared/pywikibot/core/pwb.py", line 35, in main
    runpy.run_path(str(path), run_name='__main__')
  File "<frozen runpy>", line 291, in run_path
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/wrapper.py", line 515, in <module>
    main()
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/wrapper.py", line 499, in main
    if not execute():
           ^^^^^^^^^
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/wrapper.py", line 486, in execute
    run_python_file(filename, script_args, module)
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/wrapper.py", line 147, in run_python_file
    exec(compile(source, filename, 'exec', dont_inherit=True),
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/login.py", line 178, in <module>
    main()
  File "/data/project/shared/pywikibot/core/pywikibot/scripts/login.py", line 141, in main
    for arg in pywikibot.handle_args(args):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/project/pywikibot/public_html/core/pywikibot/bot.py", line 1010, in handle_args
    pywikibot.Site()
  File "/data/project/pywikibot/public_html/core/pywikibot/__init__.py", line 255, in Site
    _sites[key] = interface(code=code, fam=fam, user=user)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/project/pywikibot/public_html/core/pywikibot/site/_apisite.py", line 133, in __init__
    self.login(cookie_only=True)
  File "/data/project/pywikibot/public_html/core/pywikibot/site/_apisite.py", line 422, in login
    raise NoUsernameError(error_msg)
pywikibot.exceptions.NoUsernameError: Logged in on wikipedia:fr via OAuth as 172.16.2.172, but expect as Framabot
CRITICAL: Exiting due to uncaught exception NoUsernameError: Logged in on wikipedia:fr via OAuth as 172.16.2.172, but expect as Framabot

There is an import warning pywikibot/comms/http.py:436: ImportWarning: No module named 'requests_oauthlib'\n warn(str(requests_oauthlib), ImportWarning)\n but is only shown if the command has -v -debug.
It would be more friendly to suggest to the used to install the missing package, the same way as it is already done when mwparserfromhell is missing.

Event Timeline

Framawiki created this task.

How can that happen? mwoauth is required for oauth support which has requests_oauthlib in their requirements.txt

I was following https://wikitech.wikimedia.org/wiki/Help:Toolforge/Raw_Kubernetes_jobs#Virtualenv_and_pywikibot on toolforge.
mwparserfromhell was missing from the default packages present on the system, and a nice error message asks to install it. But if user is using OAuth, pywikibot does not suggest installing mwoauth / requests_oauthlib.
We can also update the doc to ask running pip install -r /data/project/shared/pywikibot/core/requirements.txt.

There is an error, but it almost invisible due to the long traceback:
ERROR: OAuth authentication not supported: No module named 'requests_oauthlib'

I think an exception should be raised here:
https://github.com/wikimedia/pywikibot/blob/master/pywikibot/comms/http.py#L437

It would be more friendly to suggest to the used to install the missing package, the same way as it is already done when mwparserfromhell is missing.

mwparserfromhell package is mandatory whereas requests_oauthlib/mwoauth aren't. I think Mpaa's suggestion is a good approach.