'System.IO.FileNotFoundException: Unable to find assembly' in clr library on Python

Question:

I am getting an assembly error in my code using in a .dll file. This is a template code from another thread, and a lot of people claimed that it worked.

import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?',os.path.isfile(file)) 
clr.AddReference(file)

There is no problem with the file path I guess since it return true on the .isfile function. Here is the output that I’m getting:

Does this filepath exist? True
  File "<stdin>", line 1, in <module>
System.IO.FileNotFoundException: Unable to find assembly 'CPUThermometerLib.dll'.
   at Python.Runtime.CLRModule.AddReference(String name)

I have checked multiple threads and none of them is giving a solution. I am using Windows 10, and moreover, my .NET framework version is ‘4.0.30319.42000’. My laptop processor is an Atom Z3537F.

Asked By: Samir Ahmane

||

Answers:

The clr module is not installed by default on the Windows python installation. You will need to run pip install clr at a prompt to have it added and then your code will be able to import it properly.

Answered By: A. S.

You need to provide the full path to the dll file, not just the file name.

If the dll file is in the same directory as your Python script, you can get the full path by using os.path.abspath('CPUThermometerLib.dll').

Full code:

import clr
import os
file = 'CPUThermometerLib.dll'
print('Does this filepath exist?', os.path.isfile(file)) 
clr.AddReference(os.path.abspath(file))
Answered By: Alan Richard
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.