Introduction#

Also included are various general function scripts that can be adapted for different tasks.

For further information about the library excluding scripts see the full code documentation.

Quick start#

git clone https://gerrit.wikimedia.org/r/pywikibot/core.git
cd core
git submodule update --init
pip install -r requirements.txt
python pwb.py <script_name>

Or to install using PyPI (excluding scripts)

pip install pywikibot
pwb <scriptname>

Our installation guide has more details for advanced usage.

Basic Usage#

If you wish to write your own script it’s very easy to get started:

import pywikibot
site = pywikibot.Site('en', 'wikipedia')  # The site we want to run our bot on
page = pywikibot.Page(site, 'Wikipedia:Sandbox')
page.text = page.text.replace('foo', 'bar')
page.save('Replacing "foo" with "bar"')  # Saves the page

Wikibase Usage#

Wikibase is a flexible knowledge base software that drives Wikidata. A sample pywikibot script for getting data from Wikibase:

import pywikibot
site = pywikibot.Site('wikipedia:en')
repo = site.data_repository()  # the Wikibase repository for given site
page = repo.page_from_repository('Q91')  # create a local page for the given item
item = pywikibot.ItemPage(repo, 'Q91')  # a repository item
data = item.get()  # get all item data from repository for this item

Script example#

Pywikibot provides bot classes to develop your own script easily:

import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import ExistingPageBot

class MyBot(ExistingPageBot):

    update_options = {
        'text': 'This is a test text',
        'summary': 'Bot: a bot test edit with Pywikibot.'
    }

    def treat_page(self):
        """Load the given page, do some changes, and save it."""
        text = self.current_page.text
        text += '\n' + self.opt.text
        self.put_current(text, summary=self.opt.summary)

def main():
    """Parse command line arguments and invoke bot."""
    options = {}
    gen_factory = pagegenerators.GeneratorFactory()
    # Option parsing
    local_args = pywikibot.handle_args(args)  # global options
    local_args = gen_factory.handle_args(local_args)  # generators options
    for arg in local_args:
        opt, sep, value = arg.partition(':')
        if opt in ('-summary', '-text'):
            options[opt[1:]] = value
    MyBot(generator=gen_factory.getCombinedGenerator(), **options).run()

if __name == '__main__':
    main()

Settings#

It is recommended to create a user-config.py file if Pywikibot is used as a site package. If Pywikibot is used in directory mode e.g. from a repository the configuration file is mandatory. A minimal sample is shown below.

# This is a sample file. You can use generate_user_files script
# to create your user-config.py file:
# pwb generate_user_files

mylang = 'en'
family = 'wikipedia'
usernames['wikipedia']['en'] = 'Test'

This sample is shipped with the repository but is not available with the site-package. For more settings use generate_user_files script or refer pywikibot.config module.

See also

Installation Manual

Internationalisation (i18n)#

Some of the framework input interaction is translated. The user interface language to be used can be set as follows:

  1. set the userinterface_lang in your user-config.py to your preferred language

  2. set environment variable PYWIKIBOT_USERINTERFACE_LANG to your preferred language

  3. default is obtained from locale.getlocale

  4. fallback is 'en' for English if all other options fails

Note

The preferred language code must follow ISO 639.

Added in version 7.0: Added to site-package distribution