问题
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:
- Right-click on the .dll
- "Properties"
- 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