python中调用 C#动态链接库问题记录

程序[摘自https://blog.csdn.net/LTG01/article/details/80700513] 
import clr
clr.FindAssembly("PythonNetTest.dll") ## 加载c#dll文件
from PythonNetTest import *    # 导入命名空间
instance = Class1() #class1是dll里面的类
print(instance.AddShort(2, 3))#一个简单的加法
instance.ShowForm()

【1】报错: AttributeError: 'module' object has no attribute 'FindAssembly'
错误原因:
python存在 clr 模块,python的pythonnet模块中,也存在clr,在import 过程中,
要用的是后者中的clr,因此要利用 pip uninstall clr
把clr模块卸载掉,再运行上面的程序就可以了。
【2】 报错: name "PythonNetTest"  has not  found
错误原因:
未将PythonNetTest.dll与.py程序,放在同一个目录下,放在同一个目录下即可。

猜你喜欢

转载自blog.csdn.net/foneone/article/details/83151892