【问题标题】:Importing .NET Standard 2.0 class library with dependencies using Python.Net使用 Python.Net 导入具有依赖项的 .NET Standard 2.0 类库
【发布时间】:2019-01-07 15:11:13
【问题描述】:

我有一个 .NET Standard 2.0 类库,我正试图从 Python 中使用它。 该库依赖于一些私有和公共 NuGet 包。

我设法从artifact on appveyor 安装了pythonnet 的最新开发版本,我可以运行以下导入代码:

import clr
import sys
sys.path.append(r'C:\....\My.Library\bin\Debug\netstandard2.0')
ref = clr.AddReference("My.Library")

上面的代码运行良好,但是当我尝试从我的模块命名空间导入时...

from My.Library import MyClass
# >>> ModuleNotFoundError: No module named 'My'

当我尝试做ref.get_ExportedTypes() 时,我得到了这个:

System.IO.FileNotFoundException:
 Die Datei oder Assembly "Google.Protobuf, Version=3.6.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604"
 oder eine Abhängigkeit davon wurde nicht gefunden.
 Das System kann die angegebene Datei nicht finden.
 bei System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
 bei System.Reflection.RuntimeAssembly.GetExportedTypes()

本质上,这是关于 NuGet 依赖项的 FileNotFoundException。 构建我的 DLL 文件/使用 NuGet 依赖项导入它的正确方法是什么?

谢谢

【问题讨论】:

  • 我也有同样的问题,刚刚在github开了一个issue:github.com/pythonnet/pythonnet/issues/711
  • 为什么不能从 nuget 下载/提取所有依赖项?您是否正在尝试将 Visual Studio 体验与 C# 相匹配?!
  • 我已经开发了 .NET Standard 类库,并想创建一个可分发的 Python 包,将我的库的 API 公开给 Python 用户。将我的库的 DLL 和所有 NuGet 依赖项放入同一目录并执行 sys.path.append(r'./allthedlls') 就足够了吗?不过,一些能够分析 DLL 依赖项并自动从 NuGet 下载和提取它们的魔术帮助函数会很棒。
  • 移动到 github.com/pythonnet/pythonnet/issues/711 因为 Stackoverflow cmets 中的格式很糟糕。

标签: python .net dll nuget python.net


【解决方案1】:

通过将所有依赖项放在一个目录中,导入可以工作。 How to get .NET Core projects to copy NuGet references to build output?中介绍了几个如何实现这一点的选项,例如dotnet publish

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-27
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2023-04-05
    • 2020-03-15
    相关资源
    最近更新 更多