Genshi
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.
Common errors
-
TemplateSyntaxError: invalid syntax in expression "${item.error}" of "choose" directivecause 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.fixConsult 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'
cause 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.fixEnsure 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
cause 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.fixFor 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'
cause 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.fixUpgrade your Genshi library to version 0.7.4 or higher to ensure compatibility with Python 3.9 and later versions.
Warnings
- breaking The 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.
- breaking The `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.
- gotcha Genshi'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.
- deprecated Genshi 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.
- gotcha Changes 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.
Install
-
pip install genshi
Imports
- MarkupTemplate
from genshi.template import MarkupTemplate
- TextTemplate
from genshi.template import TextTemplate
- TemplateLoader
from genshi.template import TemplateLoader
- Markup
from genshi.template import Markup
from genshi import Markup
Quickstart
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)