Registry / web-framework / genshi

genshi

JSON →
library0.7.11pypypi✓ verified 87d ago

Genshi is a Python library that provides an integrated set of components for parsing, generating, and processing HTML, XML, or other textual content for output generation on the web. It features a template language inspired by Kid. The library is actively maintained, with version 0.7.10 released in November 2025, and generally sees several patch releases per year.

pip install genshi
INSTALL
IMPORT
SIG · GENSHI
G
genshi
web-frameworkpythonv0.7.11
Install
1.8s avg
Import
127ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.11 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.129s · 19.6MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.8s · import 0.126s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MarkupTemplate
from genshi.template import MarkupTemplate
For XML-based templates (e.g., HTML, XHTML).
TextTemplate
from genshi.template import TextTemplate
For plain text templates.
TemplateLoader
from genshi.template import TemplateLoader
To load templates from files or directories, often with caching.
Markup
from genshi import Markup
from genshi.template import Markup
The `Markup` class for explicit safe string creation is directly under `genshi`, not `genshi.template`.

This quickstart demonstrates how to create a simple HTML template using `MarkupTemplate`, pass data to it, and render the output. It showcases basic variable substitution (`py:content` and `${expression}`) and iteration (`py:for`).

from genshi.template import MarkupTemplate # Define a simple Genshi markup template string template_string = ''' <html xmlns:py="http://genshi.edgewall.org/"> <head> <title py:content="title"></title> </head> <body> <p>Hello, <em py:content="name"></em>!</p> <ul> <li py:for="item in items">${item}</li> </ul> </body> </html> ''' # Create a MarkupTemplate object tmpl = MarkupTemplate(template_string) # Data to render into the template data = { 'title': 'Genshi Example', 'name': 'World', 'items': ['Apple', 'Banana', 'Cherry'] } # Generate the output stream stream = tmpl.generate(**data) # Render the stream to a string output = stream.render('html') print(output)
Debug
Known issues
breakingThe standard library's `cgi` module, previously used by Genshi benchmarks and potentially by users in older web contexts, is slated for removal in Python 3.13. Direct usage of `cgi` within Genshi applications will break upon upgrading to Python 3.13.
fix
Migrate from the `cgi` module to modern web frameworks (e.g., Flask, Django) or use a community-maintained `legacy-cgi` package if adhering to CGI.
affects: <= 0.7.9 (if using internal cgi features), all versions (if application uses cgi)
breakingThe `element.getchildren()` method was removed from Python's standard library `xml.etree.ElementTree` in Python 3.9. Older versions of Genshi might encounter `AttributeError` if they implicitly or explicitly relied on this method.
fix
Upgrade Genshi to version 0.7.6 or newer, which explicitly removed the use of `element.getchildren()`. Alternatively, manually update older code to use `list(elem)` or direct iteration over elements.
affects: < 0.7.6
gotchaGenshi's default variable lookup mode changed from 'lenient' to 'strict' in version 0.5. In 'strict' mode, accessing an undefined variable in a template raises an `UndefinedError` immediately, instead of returning an `Undefined` object.
fix
Ensure all variables passed to templates are defined, or explicitly set `allow_undefined=True` when initializing `MarkupTemplate` or `TextTemplate`, or `allow_lookup_exceptions=False` when creating a `TemplateLoader` for lenient behavior.
affects: 0.5 and later
deprecatedGenshi versions prior to 0.7.8 could emit deprecation warnings related to attempts to import `Ellipsis` and `Str` for backward compatibility with older Python versions.
fix
Upgrade to Genshi 0.7.8 or newer, which silences these deprecation warnings.
affects: < 0.7.8
gotchaChanges in Python 3.9's Abstract Syntax Tree (AST) for `slice`, `Index`, and `ExtSlice` classes could lead to template processing issues in older Genshi versions.
fix
Upgrade to Genshi 0.7.4 or newer, which added support for these Python 3.9 AST changes.
affects: < 0.7.4 (when running on Python 3.9+)
Errors
Common errors & fixes
TemplateSyntaxError: invalid syntax in expression "${item.error}" of "choose" directive
Incorrect attribute name used in template directives, such as `py:choose error="..."` instead of `py:choose test="..."`. Python keywords used as variable names can also cause this.
fix
Consult Genshi documentation for correct directive syntax (e.g., use `test` attribute for `py:choose` and `py:when`). Avoid using Python reserved keywords as template variable names (e.g., `class`).
AttributeError: 'NoneType' object has no attribute 'strip'
This often occurs when an expression in the template attempts to call a method (like `strip()`) on a variable that is `None` or `Undefined`. This is especially common in 'strict' error handling mode.
fix
Ensure all variables accessed in the template are defined and have the expected type, or use `value_of(name, default='')` for safe access with a default value. Alternatively, configure Genshi for 'lenient' error handling, though this can mask underlying issues.
DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
Your application or an underlying dependency (like Genshi's internal benchmarks in older versions) is importing or using the `cgi` module, which is being removed from the Python standard library in version 3.13.
fix
For Genshi's internal use, ensure you are on version 0.7.10 or newer. For your own code, migrate away from `cgi` to a modern web framework or a compatible third-party `legacy-cgi` package if necessary for backward compatibility.
module '_ast' has no attribute 'Index'
This error specifically occurs when running older versions of Genshi (prior to 0.7.4) on Python 3.9 or newer, due to changes in Python's internal Abstract Syntax Tree (AST) structure.
fix
Upgrade your Genshi library to version 0.7.4 or higher to ensure compatibility with Python 3.9 and later versions.
Upgrade
Version history
0.7.11latest on PyPI · released May 17, 2026
Audit
Dependencies
setuptoolsoptionalUsed as a build backend for packaging.
babeloptionalOptional dependency for internationalization and localization features.
Agent activity
2 hits · last 30 days
node
2
Resources
genshi — pip install genshi · libregistry