程序[摘自https://blog.csdn.net/LTG01/article/details/80700513]
import clr
clr.FindAssembly("PythonNetTest.dll") ## 加載c#dll文件
from PythonNetTest import * # 導(dǎo)入命名空間
instance = Class1() #class1是dll里面的類(lèi)
print(instance.AddShort(2, 3))#一個(gè)簡(jiǎn)單的加法
instance.ShowForm()
【1】報(bào)錯(cuò): AttributeError: 'module' object has no attribute 'FindAssembly'
錯(cuò)誤原因:
python存在 clr 模塊,python的pythonnet模塊中,也存在clr,在import 過(guò)程中,
要用的是后者中的clr,因此要利用 pip uninstall clr
把clr模塊卸載掉,再運(yùn)行上面的程序就可以了。
【2】 報(bào)錯(cuò): name "PythonNetTest" has not found
錯(cuò)誤原因:
未將PythonNetTest.dll與.py程序,放在同一個(gè)目錄下,放在同一個(gè)目錄下即可。