问题描述
我正在尝试通过 PythonNet 在 Jupyter Notebook python 脚本中使用 .NET Core 库。最近添加了对 .NET Core 的支持(请参阅 https://github.com/pythonnet/pythonnet/issues/984#issuecomment-778786164),但我仍然收到 No module named 'TestAppCore'
错误。
我在 PythonNet 中使用 .NET Framework 库没有问题,只有 .NET Core。对诊断和解决问题的任何帮助将不胜感激。
我尝试使用的 C# 库是一个简单的类库项目,根本没有任何依赖项。完整代码如下:
namespace TestAppCore
{
public class Foo
{
public int ID { get; set; }
public Foo(int id)
{
ID = id;
}
public int Add(int a,int b)
{
return a + b;
}
}
}
这是python脚本:
from clr_loader import get_coreclr
from pythonnet import set_runtime
rt = get_coreclr("D:\src\Test.runtimeconfig.json")
set_runtime(rt)
import clr
import sys
sys.path.append(r"D:\src\TestAppCore")
clr.AddReference(r"TestAppCore")
from TestAppCore import Foo
foo = Foo(5)
print(foo.ID)
res = foo.Add(1,2)
print(res)
输出如下:
最后,这是我正在使用的运行时配置:
{
"runtimeOptions": {
"tfm": "netcoreapp3.1","framework": {
"name": "Microsoft.NETCore.App","version": "3.1.0"
}
}
}
- .NET 核心:3.1
- python 版本:3.7
- pythonnet:3.0.0.dev1
- clr-loader: 0.1.6
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)