Search code examples
ironpythonpython.net

Why can I access this API from IronPython but not Python.NET?


I’m working on a project that uses the Tekla EPM Open API.

I have no difficulty interfacing with it using IronPython, but have had less luck using Python.NET, which I would prefer for its greater compatibility with other modules.

The API documentation states:

There is only one function in the dll file that can be called. The c-style declaration is:

char *_FabSuiteXML( char * );

Accordingly, the following (obfuscated) code works as expected in IronPython (3.4.0a1, .NETFramework v4.6 on .NET Framework 4.8.4360.0 (64-bit) on win32):

import clr
import sys
sys.path.append('[…]\\Tekla\\Tekla EPM')
clr.AddReference("FabSuiteAPI")
import FabSuite

interface = FabSuite.FabSuiteAPI.FabSuiteAPI()

connection_string = '''
<FabSuiteXMLRequest>
<Connect>
<IPAddress>XXX.XXX.X.XX</IPAddress>
<PortNumber>XXX</PortNumber>
<Username>XXXXXX</Username>
</Connect>
</FabSuiteXMLRequest>'''

connection_response = interface.FabSuiteXML(connection_string)

print(interface)
#Yields: <FabSuite.FabSuiteAPI.FabSuiteAPI object at 0x000000000000002B [FabSuite.FabSuiteAPI.FabSuiteAPI]>

print(connection_response)
#Yields:
"""
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<FabSuiteXMLResponse xmlns="http://www.fabsuite.com/xml/fabsuite-xml-response-v0108.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Lang>en</Lang>
  <Connect>
    <Successful>1</Successful>
    <FabSuiteMajorVersion>XXXX</FabSuiteMajorVersion>
    <FabSuiteMinorVersion>XXXX</FabSuiteMinorVersion>
  </Connect>
"""

However, in (ordinary) python (3.9.5) using Python.NET, I receive the error:

Traceback (most recent call last):
  File "[…]\overflow_question_code1.py", line 15, in <module>
    connection_response = interface.FabSuiteXML(connection_string)
AttributeError: 'FabSuiteAPI' object has no attribute 'FabSuiteXML'

And indeed, python’s help() shows only those methods inherited from System.Object for the FabSuiteAPI object.

Any idea why the key method is not available in Python.NET?

Related, unhelpful Question: Python: AttributeError: 'module' object has no attribute 'AddReference'?


Solution

  • Python.NET 2.5 does not support COM interop. Here's the tracking issue.

    There's a long worded workaround using .NET reflection.

    BTW, if COM interop is the reason for using Python.NET you might in fact be better with a native Python COM client library like win32com