doc = xml.dom.minidom.Document()
el = doc.createElementNS('http://example.net/ns', 'ex:el')
el.setAttribute('xmlns:ex', 'http://example.net/ns')
doc.appendChild(el)
print(doc.toprettyxml())
How to generate XML documents with namespaces in Python