You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Help:Toolforge/Pywikibot: Difference between revisions

From Wikitech-static
Jump to navigation Jump to search
imported>JJMC89
imported>BryanDavis
(→‎Setup web-space: Remove section. Folks can read Help:Toolforge/Web for webservice things.)
Line 1: Line 1:
{{Toolforge nav}}
{{Toolforge nav}}
{{caution|This page may contain inaccuracies. It is currently being edited and redesigned for better readability. For further information, please see: [[phab:T134495]]}}
{{caution|This page may contain inaccuracies. It is currently being edited and redesigned for better readability. For further information, please see: [[phab:T134495]]}}
{{warning|This page includes samples of running pywikibot under Python2 (using <code>python</code> rather than <code>python3</code>). Pywikibot is actively deprecating Python2 ([[phab:T213287]]).}}


The '''[[mw:Manual:Pywikibot|Pywikibot Framework]]''' is a collection of Python tools that automate work on MediaWiki sites. Please review [[mw:Manual:Pywikibot/Installation]] first.
The '''[[mw:Manual:Pywikibot|Pywikibot Framework]]''' is a collection of Python tools that automate work on MediaWiki sites. Please review [[mw:Manual:Pywikibot/Installation]] first.
Line 17: Line 16:


1. Become your tool-account
1. Become your tool-account
maintainer@tools-login:~$ become toolname
<syntaxhighlight lang="shell-session">
maintainer@tools-login:~$ become toolname
</syntaxhighlight>


2. In your home directory, create (or edit, if it exists already) a '.bash_profile' file to include the following line. The path should be on one line, though it may appear to be on multiple lines depending on your screen width. When you save the .bash_profile file, your settings will be updated for all future shell sessions:
2. In your home directory, create (or edit, if it exists already) a '.bash_profile' file to include the following line. The path should be on one line, though it may appear to be on multiple lines depending on your screen width. When you save the .bash_profile file, your settings will be updated for all future shell sessions:


export PYTHONPATH=/data/project/shared/pywikibot/stable:/shared/pywikibot/stable/scripts
<syntaxhighlight lang="sh">
export PYTHONPATH=/data/project/shared/pywikibot/stable:/data/project/shared/pywikibot/stable/scripts
</syntaxhighlight>


3. Import the path settings into your current session:
3. Import the path settings into your current session:
 
<syntaxhighlight lang="shell-session">
tools.tool@tools-login$ source .bash_profile
tools.tool@tools-login$ source .bash_profile
</syntaxhighlight>


4. In your home directory, create a subdirectory named '.pywikibot' (the '.' is important!) for bot-related files:
4. In your home directory, create a subdirectory named '.pywikibot' (the '.' is important!) for bot-related files:
<syntaxhighlight lang="shell-session">
tools.tool@tools-login$ mkdir $HOME/.pywikibot
</syntaxhighlight>
[[File:Python_-data-project-shared-pywikibot-core-generate_user_files-py.png|thumb|example of configuration for commons.wikimedia.org]]


tools.tool@tools-login$ mkdir .pywikibot
[[File:Python_-data-project-shared-pywikibot-core-generate_user_files-py.png|thumb|example of configuration for commons.wikimedia.org]]
5. Configure Pywikibot.
5. Configure Pywikibot.


To create configuration files, use the following command and then follow the instructions. You may also use an existing configuration file (e.g., 'user-config.py') that works on another system by copying it into your .pywikibot directory:
To create configuration files, use the following command and then follow the instructions. You may also use an existing configuration file (e.g., 'user-config.py') that works on another system by copying it into your .pywikibot directory:
tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/generate_user_files.py
<syntaxhighlight lang="shell-session">
tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/generate_user_files.py
</syntaxhighlight>


6. Test out your setup.
6. Test out your setup.
In general, all jobs should be [[#Submitting, managing and scheduling jobs on the grid|run on the grid]], but it's fine to test your setup on the command line:
In general, all jobs should be [[#Submitting, managing and scheduling jobs on the grid|run on the grid]], but it's fine to test your setup on the command line. You should see the following terminal output (or something similar):
tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/scripts/version.py
<syntaxhighlight lang="shell-session">
You should see the following terminal output (or something similar):
tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/scripts/version.py
Pywikibot: [https] r-pywikibot-core.git (df69134, g1, 2020/03/30, 11:17:54, OUTDATED) Python: 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516]
Pywikibot: [https] r-pywikibot-core.git (df69134, g1, 2020/03/30, 11:17:54, OUTDATED)
Note that you do not run scripts using pwb.py, but run scripts directly, e.g., <code>python3 /data/project/shared/pywikibot/stable/scripts/version.py</code>. Setting <tt>PYTHONPATH</tt> means that you no longer need pwb.py to make, say, <code>import pywikibot</code> work.
Release version: 3.1.dev0
requests version: 2.12.4
  cacerts: /etc/ssl/certs/ca-certificates.crt
    certificate test: ok
Python: 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516]
</syntaxhighlight>


If you need to use multiple user-config.py files, you can do so by adding -dir:<path where you want your user-config.py> to every python command. To use the local directory, use -dir:. (colon dot).
Note that you do not run scripts using pwb.py, but run scripts directly, e.g., <code>python3 /data/project/shared/pywikibot/stable/scripts/version.py</code>. Setting <tt>PYTHONPATH</tt> means that you no longer need the pwb.py helper script to make, say, <code>import pywikibot</code> work.
 
If you need to use multiple user-config.py files, you can do so by adding <tt>-dir:<path where you want your user-config.py></tt> to every python command. To use the local directory, use <tt>-dir:.</tt> (colon dot).


For more information about Pywikibot, please see the [[mw:Pywikibot|Pywikibot documentation]]. The pywikibot mailing list (pywikibot{{@}}lists.wikimedia.org) and IRC (irc://irc.freenode.net/pywikibot) channel are good places to go for additional help. Other useful information about using the centralized 'core' files is available here: [[User:Russell Blau/Using pywikibot on Labs]]
For more information about Pywikibot, please see the [[mw:Pywikibot|Pywikibot documentation]]. The pywikibot mailing list (pywikibot{{@}}lists.wikimedia.org) and IRC (irc://irc.freenode.net/pywikibot) channel are good places to go for additional help. Other useful information about using the centralized 'core' files is available here: [[User:Russell Blau/Using pywikibot on Labs]]
Line 50: Line 65:
== Setup pywikibot on Toolforge (locally) ==
== Setup pywikibot on Toolforge (locally) ==
{{anchor|Setup pywikibot on Labs (locally)}}
{{anchor|Setup pywikibot on Labs (locally)}}
If you want to use the compat branch, we highly recommend installing it locally (it's almost impossible to use the shared files correctly and, if you try, you might leak session cookies to a location where anyone can read them, you might need additional libraries, etc.). For core, you can also install the files locally -- this would allow you to upgrade whenever it suits you, instead of always running the latest version.
Installing pywikibot local to your tool allows you to upgrade whenever it suits you, instead of always running the latest version.
 
=== Installing core ===
Similar to the instructions given in [http://lists.wikimedia.org/pipermail/pywikibot/2013-August/008168.html this mail] do:


=== {{Anchor|Installing core}} Clone pywikibot git repo ===
Clone the 'core' git repository:
Clone the 'core' git repository:
$ git clone --recursive --branch stable "https://gerrit.wikimedia.org/r/pywikibot/core" pywikibot-core
<syntaxhighlight lang="shell-session">
$ cd pywikibot-core
$ git clone --recursive --branch stable "https://gerrit.wikimedia.org/r/pywikibot/core" $HOME/pywikibot-core
 
</syntaxhighlight>
then you can compress the git repository by running
$ git gc --aggressive --prune
$ cd scripts/i18n/
$ git gc --aggressive --prune
 
which results in a repo of size ~9MB.
 
You have 2 choices on how you want to proceed now and setup core. You can do so by using an additional tool called <code>virtualenv</code> and install it as module into a virtual environment, or you can run it from sources - similiar like compat - by using the integrated <code>pwb.py</code> wrapper. For the second method no installation is needed.
 
{{anchor|virtualenv}}
;install as module - virtualenv
 
If you would like to ''install a local version'' of the 'core' branch, we recommend that you use virtualenv, which is particularly useful if your code uses a lot of externals (e.g. IRC bots, image handling bots, etc.).
 
To set up the Pywikibot core branch from cloned repo:
 
Create a virtualenv. You can call it whatever you'd like (e.g., 'pwb', in this example); shorter names are easier:
$ virtualenv pwb
 
This will install Python v2.7. To install the version 3:
$ virtualenv -p /usr/bin/python3 pwb
 
Activate it
$ source ~/pwb/bin/activate
and then do the following, which basically installs pwb-core as a symlink. This way, if you modify the directory, you don't need to install it again. This will also call python generate_user_files.py:
$ cd pywikibot-core
$ python setup.py develop
 
To use the code from outside the virtual environment (e.g. to submit jobs to the grid engine), use:
$ /data/project/tooluser/pwb/bin/python /data/project/tooluser/path/to/script.py
or
$ $HOME/pwb/bin/python /home/path/to/script.py
Note: If you want to run a script in interactive mode to debug, you'll need to run <code>source ~/pwb/bin/activate</code> first.
 
; run from sources - pwb.py wrapper
After cd'ing into pywikibot-core, run
 
$ python pwb.py login.py


which will ask a series of questions on how you want to configure your local copy. This will generate the required config files for you. Alternatively, if you have already config file from previous version, you can copy those existing config files into the pywikibot-core directory.
=== Setup a Python virtual environment for library dependencies ===
{{Anchor|virtualenv}}{{Anchor|install as module - virtualenv}}
When using a local pywikibot install, we recommend that you use a [https://docs.python.org/3/tutorial/venv.html Python virtual environment] (venv) to manage Python library dependencies. The Toolforge environment does provide system packages for many Python libraries, but these are installed using Debian packages which means that they are often older versions and not likely to be upgraded often.


Some bot scripts require extra packages to be installed -- see the file externals/README for more details.
Create a venv. You can give this venv any name you would like. We will use 'pwb' in this example.
<syntaxhighlight lang="shell-session">
$ python3 -m venv $HOME/pwb
</syntaxhighlight>


=== compat ===
Once you have created the venv, you can "activate" it to setup your shell's $PATH so that the <code>python3</code> and <code>pip3</code> binaries in the virtual environment are used by default.
<syntaxhighlight lang="shell-session">
$ source $HOME/pwb/bin/activate
(pwb) $
</syntaxhighlight>


Clone the 'compat' git repository:
Now that the venv is created and active for your current shell session, we can install the pywikibot code from the git clone we made earlier into this venv. This basically installs the pywikibot core code as a symlink in the venv. This way, if you modify the directory, you don't need to install it again.
<syntaxhighlight lang="shell-session">
(pwb) $ pip3 install --upgrade pip
...
Successfully installed pip-20.0.2
(pwb) $ pip3 install --upgrade setuptools
...
Successfully installed setuptools-46.1.3
(pwb) $ cd $HOME/pywikibot-core
(pwb) $ python3 setup.py develop
...
Finished processing dependencies for pywikibot==3.0.20200326.dev0
</syntaxhighlight>


$ git clone --recursive https://gerrit.wikimedia.org/r/pywikibot/compat.git pywikibot-compat
=== Using the virtual environment without activating it ===
 
To use the code from outside the virtual environment (for example to submit jobs to the grid engine), use the full paths to the <code>python3</code> inside your venv directory and the full path to the script you want to run:
Now you have to setup pywikibot, by running <code>login.py</code> (in fact running any bot script – like e.g. your favourite one – works):
<syntaxhighlight lang="shell-session">
 
$ $HOME/pwb/bin/python3 $HOME/path/to/script.py
$ cd pywikibot-compat
</syntaxhighlight>
$ python login.py -all
 
roughly as described in the [[#Installing core|Installing core]] section above.
 
You may setup all externals manually if you want - but this is not needed in ''compat''; see [[mw:Manual:Pywikibot/Installation#Dependencies]] for further info. If you do not install them, you may be asked to install some [[Help:Toolforge/FAQ#My Tool requires a package that is not currently installed in Toolforge. How can I add it?|extra packages]] depending on what scripts you run.
 
You will also have to enter the password for your bot eventually.
 
Now you have finished the configuration of ''compat'' and can continue setting up the webspace and jobs to execute.
 
== Setup web-space ==
 
If you want to provide data for download, you need to start a webservice; see the [[#Web services|section "Web services"]] for how to do that.
 
Per default, the directory listing on http://tools.wmflabs.org/TOOLNAME is disabled. See the [[#Example configurations|example]] under "Directory or file index" for how to enable that.
 
If you run a bot with the <code>-log</code> option, you will find the log files within the <tt>logs/</tt> directory. If you want to allow users to access it from the web, do
$ cd ~/public_html
$ mkdir logs
$ cd logs
$ ln -s ~/pywikibot-core/logs core
If you want a specific file type to be handled different by your browser, e.g. <tt>.log</tt> files like text files, see the example under "Header, mimetype, error handler" for how to configure that and (don't forget to) clear your browser's cache afterwards.
 
Next you might want to consider your <tt>cgi-bin</tt> directory:
$ cd ~/cgi-bin
follow the hints given at [[Nova Resource:Tools/Help#Logs]] ''exactly'', e.g. even the two commands
$ /usr/bin/python      # valid
$ /usr/bin/env python  # in-valid
work and do the same in shell, only the first one is valid and works here, the second is invalid! Another point to mention is that [[User:Magnus_Manske/Migrating_from_toolserver#PHP|PHP scripts go into public_html, not cgi-bin]]. Python scripts on the other hand [[Nova_Resource:Tools/Help#Published_directories|can be placed in public_html or cgi-bin]] as you wish. I would recommend to use <tt>public_html</tt> for documents and keep it listable, whereas <tt>cgi-bin</tt> should be used for CGI scripts and be protected (not listable).


== Setup job submission ==
== Setup job submission ==
After installing, you can run your bot directly via a shell command, though this is highly discouraged. You should use the grid to run jobs instead.
After installing, you can run your bot directly via a shell command, though this is highly discouraged. You should use the grid to run jobs instead.


In order to setup the submission of the jobs you want to execute and use the grid engine you should first consider [[Nova Resource:Tools/Help#Submitting, managing and scheduling jobs on the grid]] and if you are familiar with the Toolserver and its architecture consult [[User:Magnus_Manske/Migrating from toolserver#qsub et al|Migrating from toolserver]] also.
In order to setup the submission of the jobs you want to execute and use the grid engine you should first read [[Help:Toolforge/Grid]].
 
In general Toolforge uses SGE and its commands like <tt>qsub</tt> et al, this is explained [[Nova Resource:Tools/Help#Submitting, managing and scheduling jobs on the grid|in this document]] which you should use in order to get an idea which command and what parameters you want to use. Please don't use the <code>-daemonize</code> parameter as it is unneeded on the grid.


To run a bot using the grid, you might want to be in the pywikibot directory (this is not needed) - which means you have to write a small wrapper script. The following example script (versiontest.sh) is used to run version.py:
To run a bot using the grid, you might want to be in the pywikibot directory (this is not needed) - which means you have to write a small wrapper script. The following example script (versiontest.sh) is used to run version.py:


$ cat versiontest.sh
<syntaxhighlight lang="shell-session">
$ cat versiontest.sh
#!/bin/bash
#!/bin/bash
cd /path/to/pywikibot
cd /data/project/shared/pywikibot/stable
python version.py
python3 version.py
</syntaxhighlight>


To submit a job, set the permissions for the script and then use the 'jsub' command to send the job to the grid:
To submit a job, set the permissions for the script and then use the 'jsub' command to send the job to the grid:
$ chmod 755 versiontest.sh
<syntaxhighlight lang="shell-session">
$ jsub -N job_name versiontest.sh
$ chmod 0755 versiontest.sh
 
$ jsub versiontest.sh
Job output will be written to output and error files in your home directory called YOURJOBNAME.out and YOURJOBNAME.err, respectively (e.g., versiontest.out and versiontest.err in this example):
</syntaxhighlight>


$ cat ~/versiontest.out
Job output will be written to output and error files in your home directory called YOURJOBNAME.out and YOURJOBNAME.err, respectively (versiontest.out and versiontest.err in this example):
<syntaxhighlight lang="shell-session">
pywikibot [https] r/pywikibot/compat (r10211, 8fe6bdc, 2013/08/18, 14:00:57, ok)
$ cat ~/versiontest.out
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
pywikibot [https] r/pywikibot/compat (r10211, 8fe6bdc, 2013/08/18, 14:00:57, ok)
[GCC 4.6.3]
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
config-settings:
[GCC 4.6.3]
use_api = True
config-settings:
use_api_login = True
use_api = True
unicode test: ok
use_api_login = True
unicode test: ok
</syntaxhighlight>


=== Example ===
=== Example ===
An [[Help:Toolforge/Grid#Submitting continuous jobs (such as bots) with 'jstart'|infinitely running job]] (e.g. irc-bot) like this (<tt>cronie</tt> entry from TS submit host):
An [[Help:Toolforge/Grid#Submitting continuous jobs (such as bots) with 'jstart'|infinitely running job]] such as an irc-bot can be started like this:


06 0 * * * qcronsub -l h_rt=INFINITY -l virtual_free=200M -l arch=lx -N script_wui $HOME/rewrite/pwb.py script_wui.py -log
<syntaxhighlight lang="shell-session">
 
$ jsub -once -continuous -l h_vmem=256M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log
becomes
</syntaxhighlight>
or shorter
<syntaxhighlight lang="shell-session">
$ jstart -l h_vmem=256M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log
</syntaxhighlight>


$ jsub -once -continuous -l h_vmem=256M -N script_wui python $HOME/pywikibot-core/pwb.py script_wui.py -log
If you experience problems with your jobs, like e.g.
or shorter
$ jstart -l h_vmem=256M -N script_wui python $HOME/pywikibot-core/pwb.py script_wui.py -log
the first expression is good for debugging. Memory values smaller than 256MB seam not to work here, since that is the minimum. If you experience problems with your jobs, like e.g.
  Fatal Python error: Couldn't create autoTLSkey mapping
  Fatal Python error: Couldn't create autoTLSkey mapping
you can try increasing the memory value - which is also needed here, because this script uses a second thread for timing and this thread needs memory too. Therefore use finally
you can try increasing the memory value:
$ jstart -l h_vmem=512M -N script_wui python $HOME/pywikibot-core/pwb.py script_wui.py -log
<syntaxhighlight lang="shell-session">
$ jstart -l h_vmem=512M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log
</syntaxhighlight>


Now in order to create a crontab follow [[Help:Toolforge/Grid#Scheduling jobs at regular intervals with cron|Scheduling jobs at regular intervals with cron]] and setup for crontab file like:
Now in order to create a crontab follow [[Help:Toolforge/Grid#Scheduling jobs at regular intervals with cron|scheduling jobs at regular intervals with cron]] and setup for crontab file like:
$ crontab -e
<syntaxhighlight lang="shell-session">
$ crontab -e
</syntaxhighlight>
and enter
and enter
PATH=/usr/local/bin:/usr/bin:/bin
<syntaxhighlight lang="text">
PATH=/usr/local/bin:/usr/bin:/bin


06 0 * * * jstart -l h_vmem=512M -N script_wui python $HOME/pywikibot-core/pwb.py script_wui.py -log
# Run script_wui.py at 00:17 UTC each day
17 0 * * * jstart -l h_vmem=512M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log
</syntaxhighlight>


== Using pip ==
== Using pip ==
The [[:en:Pip (package manager)|pip]] package manager is not installed on the Toolforge servers, but it can be used through the use of virtual environments. The first step is to create a virtual environment, and get the latest version of <code>pip</code> installed in it:
The [[:en:Pip (package manager)|pip]] package manager is not installed for global use on the Toolforge servers, but it can be used through the use of virtual environments. The first step is to create a virtual environment, and get the latest version of <code>pip</code> installed in it:


<source lang="bash">
<source lang="shell-session">
$ virtualenv -p python3 venv
$ python3 -m venv venv
$ source venv/bin/activate
$ source venv/bin/activate
$ pip install --upgrade pip
$ pip3 install --upgrade pip
</source>
</source>


Installing specific packages from <code>pip</code> is as simple as loading the environment and then running the <code>pip install</code> command, for example:
Installing specific packages from <code>pip3</code> is as simple as loading the environment and then running the <code>pip3 install</code> command, for example:


<source lang="bash">
<source lang="shell-session">
$ source venv/bin/activate
$ source venv/bin/activate
$ pip install PACKAGENAME
$ pip3 install PACKAGENAME
</source>
</source>


Lastly, running a pywikibot script that depends on a <code>pip</code> package will also require loading the environment first, for instance:
Lastly, running a pywikibot script that depends on a <code>pip</code> package will also require loading the environment first, for instance:


<source lang="bash">
<source lang="shell-session">
$ source venv/bin/activate
$ source venv/bin/activate
$ python foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"
$ python3 foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"
</source>
</source>


The venv does not get automatically activated in Grid job submissions. Two common workarounds are having wrapping shell scripts that activates the venv, or use absolute paths to the binaries within:
The venv does not get automatically activated in Grid job submissions. Two common workarounds are having wrapping shell scripts that activates the venv, or use absolute paths to the binaries within:


<source lang="bash">
<source lang="shell-session">
$ jstart -N jobname venv/bin/python foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"
$ jstart -N jobname venv/bin/python3 foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"
</source>
</source>


[[Category:Toolforge|Pywikibot]]
[[Category:Toolforge|Pywikibot]]

Revision as of 23:29, 31 March 2020

Warning Caution: This page may contain inaccuracies. It is currently being edited and redesigned for better readability. For further information, please see: phab:T134495

The Pywikibot Framework is a collection of Python tools that automate work on MediaWiki sites. Please review mw:Manual:Pywikibot/Installation first.

The stable version of the Pywikibot 'core' branch (formerly 'rewrite') is accessible at /shared/pywikibot/stable. If you are a developer and/or would like to control when the code is updated, you may also choose to install 'core' locally in your tool directory.

Note that the shared 'core' code consists only of the source files; each bot operator will need to create his or her own configuration files (such as 'user-config.py') and set up a PYTHONPATH and other environment variables. Please see Using the shared Pywikibot files for more information.

Using the shared Pywikibot files (recommended setup)

For most purposes, using the centralized 'core' files is recommended. The shared files are available at /data/project/shared/pywikibot/stable, and steps for configuring your tool account are provided below. The configuration files themselves are stored in your tool account in the $HOME/.pywikibot directory, or another directory, where they can be used via the -dir option (all of this is described in more detail in the instructions).

If you are a developer and/or would like to control when the code is updated, or if you would like to use the 'compat' branch instead of 'core' (not all the Pywikibot scripts have been ported to 'core'), please see Installing Pywikibot locally for instructions.

To set up your Tools account to use the shared 'core' framework:

1. Become your tool-account

maintainer@tools-login:~$ become toolname

2. In your home directory, create (or edit, if it exists already) a '.bash_profile' file to include the following line. The path should be on one line, though it may appear to be on multiple lines depending on your screen width. When you save the .bash_profile file, your settings will be updated for all future shell sessions:

export PYTHONPATH=/data/project/shared/pywikibot/stable:/data/project/shared/pywikibot/stable/scripts

3. Import the path settings into your current session:

tools.tool@tools-login$ source .bash_profile

4. In your home directory, create a subdirectory named '.pywikibot' (the '.' is important!) for bot-related files:

tools.tool@tools-login$ mkdir $HOME/.pywikibot
example of configuration for commons.wikimedia.org

5. Configure Pywikibot.

To create configuration files, use the following command and then follow the instructions. You may also use an existing configuration file (e.g., 'user-config.py') that works on another system by copying it into your .pywikibot directory:

tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/generate_user_files.py

6. Test out your setup. In general, all jobs should be run on the grid, but it's fine to test your setup on the command line. You should see the following terminal output (or something similar):

tools.tool@tools-login$ python3 /data/project/shared/pywikibot/stable/scripts/version.py
Pywikibot: [https] r-pywikibot-core.git (df69134, g1, 2020/03/30, 11:17:54, OUTDATED)
Release version: 3.1.dev0
requests version: 2.12.4
  cacerts: /etc/ssl/certs/ca-certificates.crt
    certificate test: ok
Python: 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516]

Note that you do not run scripts using pwb.py, but run scripts directly, e.g., python3 /data/project/shared/pywikibot/stable/scripts/version.py. Setting PYTHONPATH means that you no longer need the pwb.py helper script to make, say, import pywikibot work.

If you need to use multiple user-config.py files, you can do so by adding -dir:<path where you want your user-config.py> to every python command. To use the local directory, use -dir:. (colon dot).

For more information about Pywikibot, please see the Pywikibot documentation. The pywikibot mailing list (pywikibot at lists.wikimedia.org) and IRC (irc://irc.freenode.net/pywikibot) channel are good places to go for additional help. Other useful information about using the centralized 'core' files is available here: User:Russell Blau/Using pywikibot on Labs

Setup pywikibot on Toolforge (locally)

Installing pywikibot local to your tool allows you to upgrade whenever it suits you, instead of always running the latest version.

Clone pywikibot git repo

Clone the 'core' git repository:

$ git clone --recursive --branch stable "https://gerrit.wikimedia.org/r/pywikibot/core" $HOME/pywikibot-core

Setup a Python virtual environment for library dependencies

When using a local pywikibot install, we recommend that you use a Python virtual environment (venv) to manage Python library dependencies. The Toolforge environment does provide system packages for many Python libraries, but these are installed using Debian packages which means that they are often older versions and not likely to be upgraded often.

Create a venv. You can give this venv any name you would like. We will use 'pwb' in this example.

$ python3 -m venv $HOME/pwb

Once you have created the venv, you can "activate" it to setup your shell's $PATH so that the python3 and pip3 binaries in the virtual environment are used by default.

$ source $HOME/pwb/bin/activate
(pwb) $

Now that the venv is created and active for your current shell session, we can install the pywikibot code from the git clone we made earlier into this venv. This basically installs the pywikibot core code as a symlink in the venv. This way, if you modify the directory, you don't need to install it again.

(pwb) $ pip3 install --upgrade pip
...
Successfully installed pip-20.0.2
(pwb) $ pip3 install --upgrade setuptools
...
Successfully installed setuptools-46.1.3
(pwb) $ cd $HOME/pywikibot-core
(pwb) $ python3 setup.py develop
...
Finished processing dependencies for pywikibot==3.0.20200326.dev0

Using the virtual environment without activating it

To use the code from outside the virtual environment (for example to submit jobs to the grid engine), use the full paths to the python3 inside your venv directory and the full path to the script you want to run:

$ $HOME/pwb/bin/python3 $HOME/path/to/script.py

Setup job submission

After installing, you can run your bot directly via a shell command, though this is highly discouraged. You should use the grid to run jobs instead.

In order to setup the submission of the jobs you want to execute and use the grid engine you should first read Help:Toolforge/Grid.

To run a bot using the grid, you might want to be in the pywikibot directory (this is not needed) - which means you have to write a small wrapper script. The following example script (versiontest.sh) is used to run version.py:

$ cat versiontest.sh
#!/bin/bash
cd /data/project/shared/pywikibot/stable
python3 version.py

To submit a job, set the permissions for the script and then use the 'jsub' command to send the job to the grid:

$ chmod 0755 versiontest.sh
$ jsub versiontest.sh

Job output will be written to output and error files in your home directory called YOURJOBNAME.out and YOURJOBNAME.err, respectively (versiontest.out and versiontest.err in this example):

$ cat ~/versiontest.out
pywikibot [https] r/pywikibot/compat (r10211, 8fe6bdc, 2013/08/18, 14:00:57, ok)
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3]
config-settings:
use_api = True
use_api_login = True
unicode test: ok

Example

An infinitely running job such as an irc-bot can be started like this:

$ jsub -once -continuous -l h_vmem=256M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log

or shorter

$ jstart -l h_vmem=256M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log

If you experience problems with your jobs, like e.g.

Fatal Python error: Couldn't create autoTLSkey mapping

you can try increasing the memory value:

$ jstart -l h_vmem=512M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log

Now in order to create a crontab follow scheduling jobs at regular intervals with cron and setup for crontab file like:

$ crontab -e

and enter

PATH=/usr/local/bin:/usr/bin:/bin

# Run script_wui.py at 00:17 UTC each day
17 0 * * * jstart -l h_vmem=512M -N script_wui python3 $HOME/pywikibot-core/pwb.py script_wui.py -log

Using pip

The pip package manager is not installed for global use on the Toolforge servers, but it can be used through the use of virtual environments. The first step is to create a virtual environment, and get the latest version of pip installed in it:

$ python3 -m venv venv
$ source venv/bin/activate
$ pip3 install --upgrade pip

Installing specific packages from pip3 is as simple as loading the environment and then running the pip3 install command, for example:

$ source venv/bin/activate
$ pip3 install PACKAGENAME

Lastly, running a pywikibot script that depends on a pip package will also require loading the environment first, for instance:

$ source venv/bin/activate
$ python3 foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"

The venv does not get automatically activated in Grid job submissions. Two common workarounds are having wrapping shell scripts that activates the venv, or use absolute paths to the binaries within:

$ jstart -N jobname venv/bin/python3 foo/bar/pwb.py SCRIPTNAME -page:"SOMEPAGE"