Ignition and Python LXML

I want to use the lxml 3rd party library within Ignition.

How do I add it and will the library be included with a gateway backup? - if not, what would be the best way to "bundle" it so that the scripting that relies on it will have the library available?

TIA

https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/python-scripting/libraries#importing-3rd-party-libraries

But I don't think you can use lxml as it appears to be bindings to a C library.

1 Like

You cannot:

The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt.

Even if you could use it, it would live in user-lib/pylib, which is not included in gateway backups.

Use java's native XML support. Helpers available here: xml.py

2 Likes

Ok, I see in the lxml description that it uses Cython and Ignition uses Jython.

Just curious, how do I tell what version of Jython Ignition uses?

edit: nevermind - it's the first thing that prints in the script console interpreter. You can also get it directly via

import sys
print sys.version_info

In general, a Python library needs to be compatible with Python 2.7 and not use native/C code in order to be used in Ignition / run on Jython.

Luckily xml.etree.ElementTree is basically the same implementation, minus the support for CDATA, but I'll just use string.format() for that anyway.

CDATA = '<![CDATA[{cdata}]]>'

desc		= ET.SubElement(tag, 'Description')
desc.text	= CDATA.format(cdata = tag_desc)

Don't use ElementTree. Like most of the jython standard library, its handling of international strings/encodings is broken.

Use the java XML standard classes. Full stop.