Manual:Pywikibot/user-config.py

From MediaWiki.org
Jump to: navigation, search
Crystal Clear action run.svg
Generate pywikibot/user-config.py
by running generate_user_files.py.
Bug blank.svg
Wikimedia Git repository has this file:
If you need more help on setting up your Pywikibot visit the IRC channel #pywikibotconnect @ freenode server or Pywikibot mailing list.

Configuration[edit | edit source]

Before you run any of the programs, you need to create a file named user-config.py in your pywikibot(core)/pywikipedia(compat) directory either one of two ways:

  1. Create the file manually (it needs at least two or three variables configured, see below) or
  2. run the script pwb.py by typing python pwb.py in the command line shell and hit ENTER (which will interactively generate the file in an interactive "wizard" style).

Location[edit | edit source]

The user-config.py is searched in following places in that order:

  • Specified via the -dir: argument (for example: -dir:/foo/bar)
  • The content of the environment variable PYWIKIBOT2_DIR (core)/PYWIKIBOT_DIR (compat)
  • The directory in which the pwb.py file is
  • In core it checks the .pywikibot directory in the application data directory (Windows) or (that directory gets automatically created)
  • In compat it checks the directory in which the wikipediatools is located

(See also: get_base_dir in core and code compat)

Below are the instructions for editing manually the content of the file.

For Wikimedia projects[edit | edit source]

Open a text editor (e.g. Notepad in Windows) and save the text file as user-config.py, in the pywikipedia folder.

If your language uses non-ASCII characters, you should use a Unicode-aware editor such as Notepad++ and save your user-config with Unicode encoding without BOM marks. Don't forget to insert

# -*- coding: utf-8 -*-

into the first line.

Add the following lines to user-config.py:

Code Explanation
family = 'wikipedia'
mylang = 'en'

xx is the code for the language code you are working on, "en" is English.[1] Family is the project name.[2]

usernames['wikipedia']['en'] = u'ExampleBot'

Your user-config.py file needs to specify the bot's username.

In this example, the user is working on English Wikipedia, and has created a bot account with the username "ExampleBot".[3] [4]

(Optional)

usernames['wikipedia']['de'] = u'BeispielBot'
usernames['wikipedia']['en'] = u'ExampleBot'
usernames['wiktionary']['de'] = u'BeispielBot'

(Optional)

If you are working on more than one Wikimedia project, you can also add several usernames.

(Optional)

console_encoding = 'utf-8'
textfile_encoding = 'unicode_escape'

(Optional, and rarely needed)

If this is the encoding used by your system. Always try without first.

(Optional)

sort_ignore_case = True

(Optional)

Some scripts may use this for sorting, e.g. solve_disambiguation.py. Default is False. Capitalized titles will preceed uncapitalized ones if this key is False or omitted, and capitalization will be disregarded by sorting if True.

Now save user-config.py again.

user-config.py examples[edit | edit source]

EksempelBot on no.wikipedia[edit | edit source]

mylang = 'no'
usernames['wikipedia']['no'] = u'EksempelBot'

console_encoding = 'utf-8'
use_api_login = True

ExampleBot on Commons[edit | edit source]

mylang = 'commons'
family = 'commons'
usernames['commons']['commons'] = u'ExampleBot'

console_encoding = 'utf-8'
use_api_login = True

ExampleBot on Test[edit | edit source]

mylang = 'test'
family = 'test'
usernames['test']['test'] = u'ExampleBot'

console_encoding = 'utf-8'
use_api_login = True

BeispielBot on de.wikipedia and de.wikibooks, with de.wikipedia as main wiki[edit | edit source]

mylang = 'de'
usernames['wikipedia']['de'] = u'BeispielBot'
usernames['wikibooks']['de'] = u'BeispielBot'

console_encoding = 'utf-8'
use_api_login = True

For non-Wikimedia wikis[edit | edit source]

Main Page: Manual:Pywikibot/Use on third-party wikis

Notes[edit | edit source]

  1. If you want to work with more than one language, choose the most common one. You can override this on the command line by using -lang:zh parameter.
  2. Meta uses 'meta' for both language code and wiki family, Commons uses 'commons' for both, and Testwiki uses 'test' for both, the multilingual wikisource uses '-' for the language. You can override this on the command line by using -family:wikibooks.
  3. The 'u' in front of the username stands for Unicode. The 'u' is required if your username contains non-ASCII characters.
  4. Note that on Linux/Unix hosts username capitalization matters! While logging in may not be an issue, testing the log in or attempting to use a bot will not use the correct cookie file and may result in anonymous access to the API. This can cause problems for private wikis that do not allow anonymous access or use third party authentication. Default usernames for mediawiki and those pulled via LDAP or other third party authentication schemes will have an uppercase character for the first letter, thus 'user' becomes 'User'.
Language: English  • Tiếng Việt