For years, writing Python in an XWiki page meant using the good old {{python}} macro, based on Jython, and so stuck with Python 2.7. Python 2 reached its end of life in 2020, so it was about time to do something about it.
The python3 macro
The new Python 3 Macro lets you write modern Python directly in your wiki pages:
{{python3}}
from datetime import date
print(f"Hello **{xcontext.getUser()}**, today is {date.today().isoformat()}")
{{/python3}}To be honest, the macro itself does very little. The heavy lifting is done by GraalPy, a Python 3 implementation which runs inside the Java virtual machine. You don't need to install Python on the server, and your scripts get access to everything the other script macros have: the script services, the XWiki API, and even Java classes. Variables are shared with the other script macros, so you can mix Python with Velocity or Groovy in the same page.
Then it got out of hand
A Python 3 macro is nice, but Python without its ecosystem of packages feels very limited. So I also added support for PyPI, mostly vibe coded with Claude Opus 5.5, I have to admit (Claude did the typing, I did the complaining). Python packages are now regular extensions: you search for them in the Extension Manager, install them on a wiki or on the whole farm, and their dependencies come with them. Then you just import them in your scripts.
There was already an old PyPI support extension, but it does not work at all anymore, and it registered the Python packages in a completely different way. Here, wheel and egg packages are a new type of extension, added to the Python path of the wiki (or farm) they are installed on, which the python3 macro relies on. Having the macro and the packages in the same project also makes things a lot easier to maintain.
There are a few limitations: only pure Python packages (no native code) can be installed, and the extension index only lists the most downloaded packages, although you can install any other one by searching outside the index. Everything is explained in the documentation.
Give it a try, and tell us what you build with it on the forum!