1. 程式人生 > >python中呼叫 C#動態連結庫問題記錄

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程式,放在同一個目錄下,放在同一個目錄下即可。