尝试使用 Python.NET 导入 c# dll 时出现“无名为模块”错误

问题描述 投票:0回答:4

我正在尝试在 Anaconda 下使用 python.NET 将 C# 模块导入 Python

这是使用 pip install pythonnet 安装的,报告“成功安装 pythonnet-2.5.2”

从那里使用 python 应该可以执行类似以下操作但无法正常工作的操作。 Jetbrains dotPeek 可以看到该模块,并且对标准 Windows dll 的调用工作正常。 我做错了什么?

clr.AddReference(R'C:\Program Files (x86)\UVtools\UVtools.Core.dll')
from UVtools.Core import About
>>ModuleNotFoundError: No module named 'UVtools'
# never gets here:
print(About.Software)

这很好用:

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
form = Form()
print(dir(form))
>> ['AcceptButton', ...

这也不起作用(尝试以不同的方式访问相同的dll)

import System
uvTools = System.Reflection.Assembly.LoadFile(R'C:\Program Files (x86)\UVtools\UVtools.Core.dll')
print(uvTools.FullName)
print(uvTools.Location)
>>UVtools.Core, Version=2.8.4.0, Culture=neutral, PublicKeyToken=null
>>C:\Program Files (x86)\UVtools\UVtools.Core.dll
# But the following all fail with the same fundamental error:
print(uvTools.UVtools.Core.About)
print(uvTools.Core.About)
print(uvTools.About)
>>AttributeError: 'RuntimeAssembly' object has no attribute 'About'

JetBrains dotPeek 可以看到模块,对我来说这意味着这不是 dll 的问题。

我确实在其他地方看到,如果尝试使用 ctype 那么您需要在 c# 基本代码中包含“[DllExport("add", CallingConvention = CallingConvention.Cdecl)]”,但我认为这不适用于 python.NET clr来电

dllimport python.net
4个回答
1
投票

我相信您的问题是您在导入语句中使用了文件扩展名。

这对我有用:

#add the directory the assembly is located in to sys.path.
assembly_path1 = r"C:\DesktopGym\src\MouseSimulatorGui\bin\Debug"
import sys
sys.path.append(assembly_path1)

import clr
#add a reference to the assembly, by name without the .dll extension.
clr.AddReference("GymSharp")

#import the class. Here this is a public class, not static.
from GymSharp import TicTacToeSharpEnvironment

# instantiate a new instance
env = TicTacToeSharpEnvironment()

# call a method
result = env.HelloWorld()

print (result)

在 C# 中,

GymSharp
是名称空间,
TicTacToeSharpEnvironment
是公共类的名称:

namespace GymSharp
{
    public class TicTacToeSharpEnvironment
    {
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

我还使用静态方法测试了我的静态类,所以这不是问题。

namespace GymSharp
{

    public static class StaticFoo
    {
        public static string StaticHelloWorld => "Static hello World";
    }

来自Python

from GymSharp import StaticFoo
print (StaticFoo.StaticHelloWorld)

0
投票

UVtools 和 Core 之间的句号阻止您访问包含 About 方法的 core 对象。您必须找到一种方法,在类和类中的方法之间只使用一个带有点或句号的名称。另请注意,如果通过程序集路径导入dll文件。该装配路径中的整个库都是可访问的。

下面 GymSharp 上的示例是如何实现和使用 pythonnet 库的一个很好的示例,下面给出了实现程序集导入:

#add the directory the assembly is located in to sys.path.
assembly_path1 = r"C:\DesktopGym\src\MouseSimulatorGui\bin\Debug"
import sys
sys.path.append(assembly_path1)

ModuleNotFoundError:没有名为“UVtools”的模块


0
投票

我也有

ModuleNotFoundError
。切换到 .NET Framework 4.8 后一切正常。我最初的项目是使用 .NET Core 3.1,它适用于这个问题,但不适合我。我也尝试过 .NET 5.0,但出现了同样的错误。

我用过:

  • pythonnet 2.5.2
  • Python 3.8.13
  • .NET Framework 4.8 的类库

0
投票

我遇到了同样的问题,我必须返回到我的 C# 文件并整理命名空间。

在您的

about.cs
文件中,命名空间设置为
UVtools.Core
,但在 Python 中,它尝试从命名空间
UVtools/Core
导入,因为点使得“UVtools”本身看起来像是一个不同的文件夹。 尝试将项目文件从“UVtools.Core”重命名为“UVtools”,并在
about.cs
文件中进行更改:

namespace UVtools.Core;

namespace UVtools;

然后重新构建装配体。在 Python 中,尝试:

from UVtools import About

这可能会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.