qual = lambda n: n if not n or ':' in n else '*[local-name() = "%s"]' % n
expr = '/'.join(qual(n) for n in expr.split('/'))
nsmap = dict((k, v) for k, v in list(tree.nsmap.items()) if k)
return tree.xpath(expr, namespaces=nsmap)
doc = """<root xmlns="http://really-long-namespace.uri"
    xmlns:other="http://with-ambivalent.end/#">
    <other:elem/>
</root>"""
tree = lxml.etree.fromstring(doc)
print(xpath_ns(tree, '/root'))
print(xpath_ns(tree, '/root/elem'))
print(xpath_ns(tree, '/root/other:elem'))
How to find XML Elements via XPath in Python in a namespace-agnostic way?