PythonNet FileNotFoundException: Unable to find assembly

亡梦爱人 提交于 2019-11-29 09:43:33

问题


I am trying to execute a Python script that uses Python For .Net (https://github.com/pythonnet/pythonnet) to load a C# library called "Kratos_3.dll" which is in the same folder as the script but the file cannot be found.

I have installed clr using "pip install pythonnet".

This is my script:

import clr
import sys
sys.path.insert(0,"C:\\dev\\proj_1\\")
clr.AddReference("Kratos_3") 

I keep getting the error

FileNotFoundException: Unable to find assembly 'Kratos_3. at Python.Runtime.CLRModule.AddReference(String name)

When I run this using IronPython it works, but I would like to get this to work using regular Python 2.7, what do I need to do?


回答1:


It turns out that even though I added the path through

sys.path.insert(0,"C:\\dev\\proj_1\\")

it still couldn't find the file because the .dll because Windows was not enabling it to load from "external sources". To fix this:

  1. Right-click on the .dll
  2. "Properties"
  3. Under "General", click "Unblock"



回答2:


Another possible reason Python for NET is unable to find lib is different platforms of Python and assemblies. Python 64-bit can't see x86 Dlls, and Python 32-bit can't see x64. Make sure your lib match Python platform, or compile them as AnyCPU.



来源:https://stackoverflow.com/questions/28840880/pythonnet-filenotfoundexception-unable-to-find-assembly

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