[英]Pythonnet TypeError when initializing class
I'm trying to access a class inside a.dll file using pythonnet and I am unable to create and instance of a class without the following error.我正在尝试使用 pythonnet 访问 a.dll 文件中的 class 并且我无法在没有以下错误的情况下创建 class 的实例。 I'm using the latest version of pythonnet (2.5.2) and Python 3.10.5.我正在使用最新版本的 pythonnet (2.5.2) 和 Python 3.10.5。
Error错误
Traceback (most recent call last):
x = IDispenser()
TypeError: interface takes exactly one argument
Python Code Python代码
import clr
import sys
dll_path = "C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8"
if dll_path not in sys.path:
sys.path.append("C:\\Program Files\\Seyonic\\Dispenser Control Monitor 3.8")
assert dll_path in sys.path
clr.AddReference("Seyonic.Dispenser")
from Seyonic.Dispenser import IDispenser
x = IDispenser()
DLL DLL
namespace Seyonic.Dispenser
{
public interface IDispenser
{
// ----------------------------------------------------------------------------
// Properties
// ----------------------------------------------------------------------------
int Data { get;}
...etc
}
}
After digging through the dll file (doesn't seem to take any arguments for using these classes) I think this is a pythonnet / python3 problem.在挖掘 dll 文件后(似乎没有使用任何 arguments 来使用这些类)我认为这是一个 pythonnet / python3 问题。 Here is an excerpt from https://github.com/DynamoDS/Dynamo/wiki/Work-in-progress-to-improve-Python-3-support :以下是https://github.com/DynamoDS/Dynamo/wiki/Work-in-progress-to-improve-Python-3-support的摘录:
Python classes cannot implement .NET interfaces (Not planned) This is probably an advanced usage scenario of Python and interoperability with .NET. Python 类无法实现 .NET 接口(未计划)这可能是 Python 的高级使用场景和与 Z3061CB0EF9EDBD908 的互操作性。 Here a Python class is defined in such a way that it implements a .NET interface.这里定义了一个 Python class,它实现了一个 .NET 接口。 The idea behind this is that instances of the class can be created and passed as arguments to methods accepting them.这背后的想法是,可以创建 class 的实例并将其作为 arguments 传递给接受它们的方法。 Here is an example of how this could look like:下面是一个例子:
import clr
clr.AddReference('SomeLibrary')
from SomeLibrary import SomeInterface, SomeClass
class MyClass(SomeInterface):
def __init__(self):
pass
inst = MyClass()
result = SomeClass.SomeMethodTakingSomeInterface(inst)
OUT = result
Given a valid library was provided, the previous code sample would work without issues in the IronPython2 engine.如果提供了一个有效的库,之前的代码示例将在 IronPython2 引擎中正常工作。 When using CPython3, however, you can get TypeError: interface takes exactly one argument or TypeError: object does not implement SomeInterface, depending on the interface and method definitions.但是,当使用 CPython3 时,您可能会遇到 TypeError: interface requires just one argument 或 TypeError: object does not implement SomeInterface,具体取决于接口和方法定义。 The required changes in Python .NET to make this work seem big when compared to the relevance we have detected for this use case, so we have decided to not plan this work yet. Python .NET 中所需的更改与我们检测到的此用例的相关性相比,使这项工作看起来很大,因此我们决定暂时不计划这项工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.