[英]ModuleNotFoundError when importing a .NET custom class in pythonnet
I'm trying to import a VERY SIMPLE custom C# class into Python using pythonnet.我正在尝试使用 pythonnet 将一个非常简单的自定义 C# 类导入 Python。 I've never used C# or VS, so It's probably some stupid mistake I'm doing.
我从未使用过 C# 或 VS,所以这可能是我在做的一些愚蠢的错误。
I have got a solid C# code base (not written by me) that I want to drive using Python.我有一个可靠的 C# 代码库(不是我写的),我想用 Python 驱动它。
I have this C# class:我有这个 C# 类:
using System;
public class MyClass
{
string text;
public MyClass(string text)
{
this.text = text;
}
public void Write()
{
Console.WriteLine(text);
}
}
In VS 2017, I've created a .NET Core Class Library project.在 VS 2017 中,我创建了一个 .NET Core 类库项目。 It compiles fine and creates a MyClass.dll file.
它可以正常编译并创建一个 MyClass.dll 文件。
Then I'm trying to import it in Python:然后我尝试在 Python 中导入它:
import sys
sys.path.append(r"C:\Users\myuser\source\repos\hello\MyClass\bin\Debug\netcoreapp2.1")
import clr
clr.FindAssembly(r"MyClass")
clr.AddReference('MyClass')
import MyClass
But I always get a "ModuleNotFoundError: No module named 'MyClass'" error.但我总是收到“ModuleNotFoundError: No module named 'MyClass'”错误。
Turns out I was compiling the DLL with .NET Core.原来我是用 .NET Core 编译 DLL。 Changing to .NET Framekwork 4.5 solved the problem.
更改为 .NET Framework 4.5 解决了这个问题。
I wish the error messages were more helpful.我希望错误消息更有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.