Steps to replicate the issue (include links if applicable):
- Create a Python file with the following code:
import pywikibot site = pywikibot.Site("commons", "commons") site.login()
- Install the latest version of Pywikibot from PyPI:
$ pip install pywikibot Collecting pywikibot Using cached pywikibot-9.0.0-py3-none-any.whl.metadata (22 kB) […] Successfully installed certifi-2024.2.2 charset-normalizer-3.3.2 idna-3.7 mwparserfromhell-0.6.6 packaging-24.0 pywikibot-9.0.0 requests-2.31.0 urllib3-2.2.1
- Try to run your Python script:
$ python3 pywikibot_error_repro.py Traceback (most recent call last): File "/private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N/.venv/lib/python3.12/site-packages/pywikibot/login.py", line 102, in __init__ user = code_to_usr.get(site.code) or code_to_usr['*'] ~~~~~~~~~~~^^^^^ KeyError: '*' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N/pywikibot_error_repro.py", line 4, in <module> site.login() File "/private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N/.venv/lib/python3.12/site-packages/pywikibot/site/_apisite.py", line 432, in login login_manager = login.ClientLoginManager(site=self, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N/.venv/lib/python3.12/site-packages/pywikibot/login.py", line 104, in __init__ raise NoUsernameError( pywikibot.exceptions.NoUsernameError: ERROR: username for commons:commons is undefined. If you have a username for that site, please add a line to user config file (user_config.py) as follows: usernames['commons']['commons'] = 'myUsername' CRITICAL: Exiting due to uncaught exception NoUsernameError: ERROR: username for commons:commons is undefined. If you have a username for that site, please add a line to user config file (user_config.py) as follows: usernames['commons']['commons'] = 'myUsername'
What happens?:
The interesting bit of that error is this line:
If you have a username for that site, please add a line to user config file (user_config.py) as follows: usernames['commons']['commons'] = 'myUsername'
If you actually follow that instruction and try re-running the original script, it will fail with the same error.
I believe I'm actually meant to add this line to user-config.py (with a hyphen) rather than user_config.py (with an underscore). If I try that, my script works.
What should have happened instead?:
Pywikibot should have given me an error message with the correct filename.
Software version (on Special:Version page; skip for WMF-hosted wikis like Wikipedia):
Here's the output of version.py:
$ python3 ~/repos/pywikibot/pwb.py version Pywikibot: [git@github.com:wikimedi] (667f97f, g18587, 2024/04/15, 14:22:39, master) Release version: 9.1.0.dev0 packaging version: 24.0 mwparserfromhell version: 0.6.6 wikitextparser version: n/a requests version: 2.31.0 cacerts: /private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N/.venv/lib/python3.12/site-packages/certifi/cacert.pem certificate test: ok Python: 3.12.0 (v3.12.0:0fb18b02c8, Oct 2 2023, 09:45:56) [Clang 13.0.0 (clang-1300.0.29.30)] PYWIKIBOT_DIR: Not set PYWIKIBOT_DIR_PWB: /Users/alexwlchan/repos/pywikibot/pywikibot/scripts PYWIKIBOT_NO_USER_CONFIG: 2 Config base dir: /private/var/folders/lp/1yl_g9ls2_g3l2tkmb6fpg0h0000gn/T/tmp.bYujOac57N
Interestingly this issue does not occur if I run the latest version of Pywikibot from Git (commit 667f97f) – I get a different error message that doesn't mention user_config.py. But I can still see the error message in the code in Git: https://gerrit.wikimedia.org/r/plugins/gitiles/pywikibot/core/+/667f97fa95ead07065c0a225e09fe37fcced1dd7/pywikibot/login.py#108
Other information (browser name/version, screenshots, etc.):
I found two other references to user_config.py which should probably be corrected at the same time: