9

我使用的是 64 位的 Windows 7。我已经设法下载并安装了pythonnet,所以

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form

工作正常。我还下载并编译/运行了一个创建大量程序集的 C# 应用程序。有问题的应用程序是 ARDrone-Control-.NET。

如何使用从 Python 生成的 DLL 文件(而不仅仅是内置的 C# 类)。

由于我从未使用过 C#(这就是我想使用 Python 库的原因),我很乐意澄清这个问题。

4

2 回答 2

13

只是提供另一种方法:

import sys
sys.path.append("C:\Path\to\your\assemblies")

clr.AddReference('MyAssembly')
from MyAssembly import MyClass

MyClass.does_something()

这假定在C:\Path\to\your\assemblies文件夹中有一个 MyAssembly.dll 文件。

所以“诀窍”是您必须将您的程序集文件夹添加到sys.path之前的clr.AddReference.

于 2012-11-05T22:33:18.450 回答
3

根据我收集到的信息,您正在尝试在 Python.Net 中加载外部程序集,我对该库几乎没有做任何工作。您应该考虑改用 IronPython,但使用 Python.Net,您可以像这样通过 .Net 的反射加载程序集

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System.Reflection import Assembly
>>> dll1 = Assembly.LoadFile("C:\Python\Python27-32\Lib\site-packages\Python.Runtime.dll")
>>> clr.Python.Runtime
<module 'Python.Runtime'>
>>> clr.Python.Runtime.PythonEngine
<class 'Python.Runtime.PythonEngine'>
于 2011-06-16T00:41:37.873 回答