PythonNet FileNotFoundException: Unable to find assembly
Asked Answered
P

2

8

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?

Platina answered 3/3, 2015 at 20:0 Comment(0)
P
20

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"
Platina answered 4/3, 2015 at 14:0 Comment(3)
I spent so much time fixing it. Nowhere I could have thought of 'Unblock'. Thanks a ton!Pitsaw
Thank you so much, could have spend hours on this!Beeeater
Great answer! Thanks for posting!Yak
S
9

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.

Sycophant answered 26/7, 2017 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.