Python 3 Macro
Last modified by Thomas Mortagne on 2026/10/10 10:42
Reference
The python3 macro, provided by the Python 3 Macro extension, executes the Python 3 script it contains. Like the other script macros, it requires programming rights by default, and it can be used inline.
{{python3}}
from datetime import date
print(f"Hello **{xcontext.getUser()}**, today is {date.today().isoformat()}")
{{/python3}}What the script prints is parsed as wiki syntax, so the page displays, with the user name in bold:
Hello XWiki.Admin, today is 2026-10-10Parameters
| Parameter | Default | What it does |
|---|---|---|
| output | true | Inserts what the script prints in the page. With false, the script is executed but displays nothing. |
| wiki | true | Parses what the script prints as wiki syntax. With false, it is displayed as plain text. |
Variables and Definitions
| What | Scope |
|---|---|
| The script context variables (xcontext, services, doc, xwiki, and the ones set by other script macros, like the groovy one) | Available as global variables of the script. |
| The global variables set by the script (except the ones starting with __) | Copied to the script context, so the following script macros, in any language, can read them. |
| The functions, classes and imported modules defined by the script | Available to the following python3 macros executed in the same request, for the same wiki and user. |
| The installed Python packages | Importable when installed on the current wiki or on the farm, see Install Python Packages. |