{"id":8193,"library":"genshi","title":"Genshi","description":"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.","status":"active","version":"0.7.10","language":"en","source_language":"en","source_url":"https://github.com/edgewall/genshi","tags":["templating","web","html","xml","markup"],"install":[{"cmd":"pip install genshi","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Used as a build backend for packaging.","package":"setuptools","optional":true},{"reason":"Optional dependency for internationalization and localization features.","package":"babel","optional":true}],"imports":[{"note":"For XML-based templates (e.g., HTML, XHTML).","symbol":"MarkupTemplate","correct":"from genshi.template import MarkupTemplate"},{"note":"For plain text templates.","symbol":"TextTemplate","correct":"from genshi.template import TextTemplate"},{"note":"To load templates from files or directories, often with caching.","symbol":"TemplateLoader","correct":"from genshi.template import TemplateLoader"},{"note":"The `Markup` class for explicit safe string creation is directly under `genshi`, not `genshi.template`.","wrong":"from genshi.template import Markup","symbol":"Markup","correct":"from genshi import Markup"}],"quickstart":{"code":"from genshi.template import MarkupTemplate\n\n# Define a simple Genshi markup template string\ntemplate_string = '''\n<html xmlns:py=\"http://genshi.edgewall.org/\">\n  <head>\n    <title py:content=\"title\"></title>\n  </head>\n  <body>\n    <p>Hello, <em py:content=\"name\"></em>!</p>\n    <ul>\n      <li py:for=\"item in items\">${item}</li>\n    </ul>\n  </body>\n</html>\n'''\n\n# Create a MarkupTemplate object\ntmpl = MarkupTemplate(template_string)\n\n# Data to render into the template\ndata = {\n    'title': 'Genshi Example',\n    'name': 'World',\n    'items': ['Apple', 'Banana', 'Cherry']\n}\n\n# Generate the output stream\nstream = tmpl.generate(**data)\n\n# Render the stream to a string\noutput = stream.render('html')\n\nprint(output)","lang":"python","description":"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`)."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"<= 0.7.9 (if using internal cgi features), all versions (if application uses cgi)"},{"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.","message":"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.","severity":"breaking","affected_versions":"< 0.7.6"},{"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.","message":"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.","severity":"gotcha","affected_versions":"0.5 and later"},{"fix":"Upgrade to Genshi 0.7.8 or newer, which silences these deprecation warnings.","message":"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.","severity":"deprecated","affected_versions":"< 0.7.8"},{"fix":"Upgrade to Genshi 0.7.4 or newer, which added support for these Python 3.9 AST changes.","message":"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.","severity":"gotcha","affected_versions":"< 0.7.4 (when running on Python 3.9+)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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`).","cause":"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.","error":"TemplateSyntaxError: invalid syntax in expression \"${item.error}\" of \"choose\" directive"},{"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.","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.","error":"AttributeError: 'NoneType' object has no attribute 'strip'"},{"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.","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.","error":"DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13"},{"fix":"Upgrade your Genshi library to version 0.7.4 or higher to ensure compatibility with Python 3.9 and later versions.","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.","error":"module '_ast' has no attribute 'Index'"}]}