{"id":10179,"library":"pywikibot","title":"Pywikibot","description":"Pywikibot is a Python library and framework for automating interactions with MediaWiki sites. It provides classes and functions to access, modify, and manage content programmatically, enabling developers to create bots for tasks like editing pages, managing categories, and extracting data. The current stable version is 11.1.0, and it follows a semi-regular release cadence, often aligning with MediaWiki updates.","status":"active","version":"11.1.0","language":"en","source_language":"en","source_url":"https://github.com/pywikibot/pywikibot","tags":["MediaWiki","bot","wiki","automation","API"],"install":[{"cmd":"pip install pywikibot","lang":"bash","label":"Install Pywikibot"}],"dependencies":[],"imports":[{"note":"While technically possible, direct imports from submodules like `pywikibot.site` or `pywikibot.page` are discouraged. `Site` and `Page` are exposed at the top level of `pywikibot` and internal submodule paths can change.","wrong":"from pywikibot.site import Site","symbol":"Site, Page","correct":"import pywikibot\n# then pywikibot.Site(), pywikibot.Page()"}],"quickstart":{"code":"import pywikibot\nimport os\n\n# --- Pywikibot requires a user-config.py file for site configuration and credentials ---\n# Create a user-config.py in your bot's directory or the pywikibot installation directory.\n# Example user-config.py content (replace with your actual info):\n# family = 'wikipedia'\n#mylang = 'en'\n#usernames['wikipedia']['en'] = 'YourBotUsername'\n#password_file = 'user-password.py' # or password['wikipedia']['en'] = 'YourBotPassword'\n\n# Use 'en', 'wikipedia' for the English Wikipedia, or other codes for different sites.\nsite = pywikibot.Site(os.environ.get('PYWIKIBOT_LANG', 'en'), os.environ.get('PYWIKIBOT_FAMILY', 'wikipedia'))\n\ntry:\n    site.login() # Attempt to log in based on user-config.py\n    print(f\"Successfully logged in to {site.family.name}:{site.lang} as {site.user().name}\")\nexcept pywikibot.exceptions.LoginError as e:\n    print(f\"Login failed: {e}. Check user-config.py and ensure credentials are correct.\")\n    # Fallback to anonymous for read-only actions if login isn't strictly needed\n    print(\"Proceeding with anonymous access for read-only operations.\")\n\npage = pywikibot.Page(site, os.environ.get('PYWIKIBOT_PAGE', 'Pywikibot'))\n\nif page.exists():\n    print(f\"\\n--- Page Info for '{page.title()}' ---\")\n    print(f\"URL: {page.full_url()}\")\n    print(f\"Page ID: {page.pageid}\")\n    print(f\"Last edited by: {page.last_updated_by.name}\")\n    print(\"Content snippet (first 200 chars):\")\n    print(page.text[:200])\nelse:\n    print(f\"\\nPage '{page.title()}' does not exist on {site.family.name}:{site.lang}.\")","lang":"python","description":"This quickstart demonstrates how to initialize a Pywikibot site, attempt to log in (essential for most operations), and retrieve basic information and content from a specified page. Before running, you MUST create a `user-config.py` file with your bot's credentials and site preferences as outlined in the official documentation."},"warnings":[{"fix":"Create and correctly configure `user-config.py` in your bot's working directory or the Pywikibot installation directory. Refer to `https://www.mediawiki.org/wiki/Manual:Pywikibot/user-config.py`.","message":"Essential Configuration via `user-config.py`. Many basic operations, especially write actions, fail without proper configuration of your username, password, and default wiki site in `user-config.py`. This file is not automatically created and is critical for operation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After initializing `site = pywikibot.Site(...)`, always call `site.login()` before attempting operations that require user authentication.","message":"Explicit Login Required. For most write operations (and even some read operations on private wikis), you must explicitly call `site.login()`. Forgetting this leads to permission errors or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the official Pywikibot documentation for the version you are using. Specifically review the migration guides if upgrading from pre-9.0.0 versions.","message":"Significant API Changes from v9.0.0 onwards. Pywikibot underwent a major rewrite, especially affecting how `Site` and `Page` objects are handled, method names, and return types. Scripts written for older versions (e.g., v8.x) will likely require substantial updates.","severity":"breaking","affected_versions":"9.0.0 and later"},{"fix":"Design your bot with appropriate delays (e.g., `time.sleep()`) between actions, especially for write operations or iterating over many pages. Monitor bot activity and adhere to the wiki's bot policy.","message":"MediaWiki API Rate Limits. Aggressive or poorly designed bots can quickly hit MediaWiki API rate limits, leading to temporary blocks from the wiki. While Pywikibot has internal throttling, it's not foolproof against rapid, high-volume requests.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refactor scripts to use the direct `pywikibot.Site()` and `pywikibot.Page()` object interaction patterns instead of relying on the older `Robot` framework.","message":"The `pywikibot.bot.Robot` class and related modules are deprecated. Developers are encouraged to use `pywikibot.Site`, `pywikibot.Page`, and other core Pywikibot objects directly.","severity":"deprecated","affected_versions":"9.0.0 and later"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `user-config.py` has correct `usernames`, `password` (or `password_file`), and `family` settings. Call `site.login()` after creating the `Site` object. Manually log in via browser for CAPTCHA/2FA if necessary, then try again.","cause":"Incorrect credentials or site specified in `user-config.py`, `site.login()` not called, or Two-Factor Authentication/CAPTCHA awaiting input.","error":"pywikibot.exceptions.LoginError: Login failed for user 'your_username'."},{"fix":"Use `import pywikibot` and then access objects via `pywikibot.Site()` and `pywikibot.Page()`.","cause":"Attempting to import internal components directly. `Site` and `Page` are exposed at the top level of `pywikibot` for convenience.","error":"ImportError: cannot import name 'Site' from 'pywikibot.site'"},{"fix":"Verify the page title and confirm its existence on the wiki (case sensitivity matters for some wikis).","cause":"The specified page title does not exist on the target wiki, or there's a typo in the title.","error":"pywikibot.exceptions.NoPageError: No page 'NonExistentPage' on site 'en:wikipedia'."},{"fix":"Consult the latest documentation for `pywikibot.Page` constructor arguments. Typically, `pywikibot.Page(site_object, 'Page Title')` is the correct form.","cause":"Using an outdated `Page` constructor signature from an older major version of Pywikibot.","error":"TypeError: Page() got an unexpected keyword argument 'title'"}]}