Using C# Assemblies from Python via pythonnet

依然范特西╮ 提交于 2019-12-03 06:52:42

Just to provide another method:

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

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

MyClass.does_something()

This assumes that in the C:\Path\to\your\assemblies folder you have a MyAssembly.dll file.

So the 'trick' is that you have to add your assemblies folder to the sys.path before clr.AddReference.

mitchellsg

From what I gather you are trying to load an external assembly in Python.Net, I have done little work with that library. You should consider using IronPython instead but using Python.Net you could load the assembly via .Net's reflection like this

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'>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!