Lxml error in running example

Trying to run openMC on a fresh ubuntu install under WSL (performed all steps in the dockerfile to install openMC).
running the pincell example gives an error related to the lxml package (seems to be an issue with the way lxml is called):

Traceback (most recent call last):
  File "build_xml.py", line 56, in <module>
    geometry.export_to_xml()
  File "/mnt/c/Users/SOC/Workspace/codes/openmc/openmc/geometry.py", line 98, in export_to_xml
    self.root_universe.create_xml_subelement(root_element, memo=set())
  File "/mnt/c/Users/SOC/Workspace/codes/openmc/openmc/universe.py", line 578, in create_xml_subelement
    cell_element = cell.create_xml_subelement(xml_element, memo)
  File "/mnt/c/Users/SOC/Workspace/codes/openmc/openmc/cell.py", line 622, in create_xml_subelement
    create_surface_elements(self.region, xml_element, memo)
  File "/mnt/c/Users/SOC/Workspace/codes/openmc/openmc/cell.py", line 613, in create_surface_elements
    xml_element.append(node.surface.to_xml_element())
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 237, in append
    self._assert_is_element(subelement)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 259, in _assert_is_element
    raise TypeError('expected an Element, not %s' % type(e).__name__)
TypeError: expected an Element, not Element

lxml version: 4.6.3
python version: 3.8.10

Has anyone encountered such an error before, or know a fix?

Hi @SOC and welcome to the community. Based on the traceback, the error is actually occurring in xml.etree.ElementTree, not lxml. Currently, lxml is only used in another part of the code. Are you able to share your Python code (or a minimal example) that demonstrates this error so I can try to reproduce?

Here is a minimal example adapted from the pincell example:

from math import log10

import numpy as np
import openmc

###############################################################################
# Create materials for the problem


helium = openmc.Material(name='Helium for gap')
helium.set_density('g/cm3', 0.001598)
helium.add_element('He', 2.4044e-4)

# Collect the materials together and export to XML
materials = openmc.Materials([helium])
materials.export_to_xml()

###############################################################################
# Define problem geometry

# Create cylindrical surfaces
fuel_or = openmc.ZCylinder(r=0.39218, name='Fuel OR')
clad_ir = openmc.ZCylinder(r=0.40005, name='Clad IR')
clad_or = openmc.ZCylinder(r=0.45720, name='Clad OR')

# Create a region represented as the inside of a rectangular prism
pitch = 1.25984
box = openmc.rectangular_prism(pitch, pitch, boundary_type='reflective')

# Create cells, mapping materials to regions
gap = openmc.Cell(fill=helium, region=+fuel_or & -clad_ir)

# Create a geometry and export to XML
geometry = openmc.Geometry([gap])
geometry.export_to_xml()

Strangely, the material export to xml is working but it is when the geometry is exported that the xml/etree/ElementTree error appeared.

When I try running this code, it seems to work just fine (also on Python 3.8). Could you run pip list in your WSL environment and paste the results here?

Here it is:

Package                Version              Location
---------------------- -------------------- ---------------------------------------
attrs                  19.3.0
Automat                0.8.0
backcall               0.2.0
beautifulsoup4         4.8.2
bleach                 3.1.1
blinker                1.4
certifi                2019.11.28
chardet                3.0.4
Click                  7.0
cloud-init             21.2
colorama               0.4.3
command-not-found      0.3
configobj              5.0.6
constantly             15.1.0
coverage               5.5
cryptography           2.8
cycler                 0.10.0
dbus-python            1.2.16
decorator              5.1.0
defusedxml             0.6.0
distro                 1.4.0
distro-info            0.23ubuntu1
entrypoints            0.3
future                 0.18.2
h5py                   3.4.0
html5lib               1.0.1
httplib2               0.14.0
hyperlink              19.0.0
idna                   2.8
importlib-metadata     1.5.0
incremental            16.10.1
iniconfig              1.1.1
ipykernel              5.2.0
ipython                7.27.0
ipython_genutils       0.2.0
ipywidgets             6.0.0
jedi                   0.18.0
Jinja2                 2.10.1
jsonpatch              1.22
jsonpointer            2.0
jsonschema             3.2.0
jupyter-client         6.1.2
jupyter-core           4.6.3
keyring                18.0.1
kiwisolver             1.3.2
language-selector      0.1
launchpadlib           1.10.13
lazr.restfulclient     0.14.2
lazr.uri               1.0.3
lxml                   4.6.3
MarkupSafe             1.1.0
matplotlib             3.4.3
matplotlib-inline      0.1.3
mistune                0.8.4
more-itertools         4.2.0
nbconvert              5.6.1
nbformat               5.0.4
netifaces              0.10.4
notebook               6.0.3
numpy                  1.21.2
oauthlib               3.1.0
openmc                 0.13.0.dev0          /mnt/c/Users/SOC/Workspace/codes/openmc
packaging              21.0
pandas                 1.3.3
pandocfilters          1.4.2
parso                  0.8.2
pexpect                4.6.0
pickleshare            0.7.5
Pillow                 8.3.2
pip                    21.2.4
pluggy                 1.0.0
prometheus-client      0.7.1
prompt-toolkit         3.0.20
py                     1.10.0
pyasn1                 0.4.2
pyasn1-modules         0.2.1
Pygments               2.10.0
PyGObject              3.36.0
PyHamcrest             1.9.0
PyJWT                  1.7.1
pymacaroons            0.13.0
PyNaCl                 1.3.0
pyOpenSSL              19.0.0
pyparsing              2.4.7
pyrsistent             0.15.5
pyserial               3.4
pytest                 6.2.5
pytest-cov             2.12.1
python-apt             2.0.0+ubuntu0.20.4.6
python-dateutil        2.8.2
python-debian          0.1.36ubuntu1
pytz                   2021.1
PyYAML                 5.3.1
pyzmq                  18.1.1
requests               2.22.0
requests-unixsocket    0.2.0
scipy                  1.7.1
SecretStorage          2.3.1
Send2Trash             1.5.0
service-identity       18.1.0
setuptools             45.2.0
simplejson             3.16.0
six                    1.14.0
sos                    4.1
soupsieve              1.9.5
ssh-import-id          5.10
systemd-python         234
terminado              0.8.2
testpath               0.4.4
toml                   0.10.2
tornado                5.1.1
traitlets              5.1.0
Twisted                18.9.0
ubuntu-advantage-tools 27.2
ufw                    0.36
unattended-upgrades    0.1
uncertainties          3.1.6
urllib3                1.25.8
wadllib                1.3.3
wcwidth                0.2.5
webencodings           0.5.1
wheel                  0.34.2
widgetsnbextension     2.0.0
zipp                   1.0.0
zope.interface         4.7.1


I tried to reproduce this environment as closely as I could, but the example script still works with no problems. I do see that you have many packages here that are not necessary for OpenMC. You might want to try creating a new Python virtual environment with just an installation of OpenMC and its dependencies and see if that solves the issue.

Problem has been solved by resetting Ubuntu and installing openmc through Anaconda.
We are not sure what the issue was.
The openmc python package was installed with sudo pip install -e ., (the -e flag does not allow pip to install with --user). this could have caused some issues with conflicting xml packages?

Problem is solved, thank you very much for your time and effort

2 Likes