pyKML
raw JSON → 0.2.0 verified Mon Apr 27 auth: no python maintenance
Python KML (Keyhole Markup Language) library for parsing, constructing, and manipulating KML files. Version 0.2.0 is the current release; it is in maintenance mode with infrequent updates.
pip install pykml Common errors
error AttributeError: 'NoneType' object has no attribute 'text' ↓
cause Accessing a child element that does not exist (e.g., Document.Placemark when Document is missing).
fix
Check for None: if root.Document: placemark = root.Document.Placemark
error ModuleNotFoundError: No module named 'lxml' ↓
cause lxml is not installed; pykml depends on it but does not declare it in install_requires.
fix
pip install lxml
Warnings
gotcha pykml requires lxml as a dependency; it is not automatically installed if you use pip without lxml already present. Install lxml explicitly. ↓
fix pip install lxml
deprecated The library uses a non-standard namespace model with capitalized 'KML' classes, which can cause confusion when working with standard lxml methods. ↓
fix Use the pykml.factory module with KML_ElementMaker for generating KML.
Imports
- KML
from pykml import KML - parser
from pykml import parser - parse wrong
from pykml import parsecorrectfrom pykml.parser import parse - kml_parse wrong
from pykml.kml_parse import ...correctfrom pykml.factory import KML_ElementMaker as KML
Quickstart
from pykml import parser
from lxml import etree
kml_str = '''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Test</name>
</Placemark>
</Document>
</kml>'''
root = parser.fromstring(kml_str)
placemark = root.Document.Placemark
print(placemark.name.text)