Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python for .NET installation: Could not load file or assembly 'clr.pyd'

Really sorry guys! But for the life of me I am nearing the breaking point of going mad trying to install Python for .NET. I read the instructions meticulously below and still can't seem to get the installation to work. I am running python on an Spyder IDE, I a have windows 7. I downloaded the files pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86. I found my python directory by using the following commands:

>>> import os
>>> os.getcwd()
'C:\\Users\\Jessica'

I dragged all the unziped files into the directory and tried to load CLR

>>> import clr as ccllrr

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'clr.pyd' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
File name: 'clr.pyd'

But I keep getting this error message. What am I doing wrong? Did a miss a step somewhere?

Here are the instructions I have been following: How to install Python for .NET on Windows

and

http://pythonnet.github.io/

like image 630
jessica Avatar asked Dec 13 '14 05:12

jessica


1 Answers

The short answer is that you need to install .NET Framework 4.0 or later.

I actually don't have .Net installed.

Yes you do. Windows 7 comes with .NET built in—but it normally comes with only 2.0, 3.0, and 3.5. (This question gives some info on that.) And you need 4.0.

I didn't think I need it to e installed as this module was a substitute that allowed me to use .Net through Python.

Yes, it allows you to use .NET through Python—but you still have to have .NET for it to use.


Anyway, instead of installing .NET 4.0, you could build Python for .NET from source to use .NET 3.5, or look for a version pre-built against 3.5 or 4.0. But installing .NET 4.0 is probably the simplest way to get yourself working.


How did I know you need 4.0?

Well, first, this error message:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'clr.pyd' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

… means the clr.pyd file is built for some newer version of .NET than the one you have. But which one? That comes from the name of the you downloaded:

pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86

The clr4.0 part means that it's for CLR version 4.0. If you knew that CLR (aka "Common Language Runtime") and ".NET Framework Runtime" were the same thing, this would be obvious, but most people don't actually know that unless they're already .NET programmers, so… not exactly your fault for not guessing.

like image 52
abarnert Avatar answered Oct 21 '22 05:10

abarnert