Python is one of the most used programming languages on Toolforge.
If you don't have a special python setup or want to use the latest versions, you can tryusing the build service, for which there's severaltutorials available.
For guidance on writing tools in Python, see e. g.Help:Toolforge/My first Flask OAuth tool,Help:Toolforge/My first Django OAuth tool and the build service basedtutorials.
For this tutorial, we will be usingprebuilt container images, the newest Python available on them ispython3.13.
toolforge webservice python3.13 shell.When using a prebuilt image (such aspython3.13) with a webservice or ajob, a Pythonvirtual environment (venv) allows the maintainers of a tool to install Python packages usingpip without affecting any other tools running in Toolforge. A venv is similar to the use ofcomposer to install PHP libraries orbundler to install ruby libraries that are local to directory rather than system wide.
A virtual environmentmust be created in the same execution environment that the venv will be used from. This means thatany virtual enviroments must be bootstrapped inside a container usingtoolforge webservice python3.13 shell or a job.
SeeToolforge webservices with Python.
Follow these instructions if you are using theToolforge Jobs framework.
You will need to run a one-time manual job to bootstrap a python venv from inside a job container. This is similar to the instructions about usingtoolforge webservice python3.13 shell when bootstrapping kubernetes webservices.
Create a script similar to this to create the venv:
#!/bin/bash# use bash strict modeset-euopipefail# delete the venv, if it already existsrm-rfpyvenv# create the venvpython3-mvenvpyvenv# activate itsourcepyvenv/bin/activate# upgrade pip inside the venv and add support for the wheel package formatpipinstall-Upipwheel# Change the following section depending on what your tool needs!# install some concrete packages# pip install requests# pip install pyyaml# or, install all packages from src/requirements.txt# pip install -r src/requirements.txt
Run the bootstrapping script in the desired python container:
tools.mytool@tools-sgebastion-11:~$lsbootstrap_venv.shbootstrap_venv.shtools.mytool@tools-sgebastion-11:~$chmodug+xbootstrap_venv.shtools.mytool@tools-sgebastion-11:~$toolforgejobsrunbootstrap-venv--command"cd$PWD && ./bootstrap_venv.sh"--imagepython3.13--waittools.mytool@tools-sgebastion-11:~$lspyvenvpyvenv
Once the venv had been created, you can run your python job as many times as needed as a manual, continuous, or periodic job:
tools.mytool@tools-sgebastion-11:~$catsrc/mytool.pyimport requestsr = requests.get('https://www.wikidata.org/wiki/Q1')print(r.status_code)tools.mytool@tools-sgebastion-11:~$toolforgejobsrunmytool--command"pyvenv/bin/python src/mytool.py"--imagepython3.13tools.mytool@tools-sgebastion-11:~$catmytool.out200
Or, if you're using a shell script as the entry point for your job, usesourcepyvenv/bin/activate before running any other commands.
For scheduling recurring or continuous jobs, seeHelp:Toolforge/Jobs framework.
Many legacy tools and code examples use Python 2.x as a runtime. Use of Python 3.x is encouraged for new code as Python 2.7stopped being maintained in 2020. Toolforge will provide some amount of support for Python 2.xthrough 2022 because Debian will be supportingPython 2.7 in Debian 10 (buster). This support will only extend to critical security patches however.
SeeHelp:Toolforge/Elasticsearch#Python considerations.
Support and administration of the WMCS resources is provided by theWikimedia Foundation Cloud Services team andWikimedia movement volunteers. Please reach out with questions and join the conversation:
Use a subproject of the#Cloud-ServicesPhabricator project to track confirmed bug reports and feature requests about the Cloud Services infrastructure itself
Read theCloud Services Blog (for the broader Wikimedia movement, see theWikimedia Technical Blog)