Movatterモバイル変換


[0]ホーム

URL:


Jump to content
Wikitech
Search

Help:Toolforge/Python

From Wikitech
<Help:Toolforge

Toolforge

[edit]

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.

You can not directly create a virtual environment in the Bookworm-based bastion servers. You must first open a Kubernetes shell usingtoolforge webservice python3.13 shell.
In addition, if you created virtual environments in older Toolforge bastions (e.g. for Grid Engine jobs), they may no longer work; you need to recreate new ones.

Virtual environments with prebuilt images

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.

Web services

SeeToolforge webservices with Python.

Jobs

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:

$HOME/bootstrap_venv.sh
#!/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.

Deprecating Python 2

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.

Elasticsearch

SeeHelp:Toolforge/Elasticsearch#Python considerations.

See also

Communication and support

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:

Discuss and receive general support
Stay aware of critical changes and plans
Track work tasks andreport bugs

Use a subproject of the#Cloud-ServicesPhabricator project to track confirmed bug reports and feature requests about the Cloud Services infrastructure itself

Read stories and WMCS blog posts

Read theCloud Services Blog (for the broader Wikimedia movement, see theWikimedia Technical Blog)

Retrieved from "https://wikitech.wikimedia.org/w/index.php?title=Help:Toolforge/Python&oldid=2249498"
Categories:

[8]ページ先頭

©2009-2025 Movatter.jp