Jump to content

Manual:Pywikibot/Cookbook/Testing the framework

From mediawiki.org

Let's try this at Python prompt:

>>> import pywikibot
>>> site = pywikibot.Site()
>>> print(site.username())
BinBot

Of course, you will have the name of your own bot there if you have set the user-config.py properly. Now, what does it mean? This does not mean this is a valid username, even less it is logged in. This does not mean you have reached Wikipedia, neither you have Internet connection. This means that Python is working, Pywikibot is working, and you have set your home wiki and username in user-config.py. Any string may be there by this point.

If you save the above code to a file called test.py:

import pywikibot
site = pywikibot.Site()
print(site.username())

and run it with python pwb.py -user:Brghhwsf test, you will get Brghhwsf.

Now try

print(site.user())

This is already a real contacting your wiki; the result is the name of your bot if you have logged in, otherwise None. For advanced use it is important that although user() is similar to a User() object instance, here it is just a string. See https://doc.wikimedia.org/pywikibot/master/api_ref/pywikibot.site.html#pywikibot.site._basesite.BaseSite.user.