5

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/

4

1 回答 1

7

简短的回答是您需要安装 .NET Framework 4.0 或更高版本

我实际上没有安装.Net。

是的你是。Windows 7 带有内置的 .NET,但通常只带有 2.0、3.0 和 3.5。(这个问题提供了一些信息。)你需要4.0。

我不认为我需要安装它,因为这个模块是允许我通过 Python 使用 .Net 的替代品。

是的,它允许您通过 Python使用.NET——但您仍然必须拥有.NET 才能使用.NET 。


无论如何,您可以从源代码为 .NET 构建 Python 以使用 .NET 3.5,而不是安装 .NET 4.0,或者寻找针对 3.5 或 4.0 预构建的版本。但安装 .NET 4.0 可能是让自己工作的最简单方法。


我怎么知道你需要 4.0?

好吧,首先,这个错误信息:

未处理的异常:System.BadImageFormatException:无法加载文件或程序集“clr.pyd”或其依赖项之一。此程序集由比当前加载的运行时更新的运行时构建,无法加载。

... 表示该clr.pyd文件是为比您拥有版本更新的 .NET 版本构建的。但是哪一个?这来自您下载的名称:

pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86

clr4.0部分表示它适用于 CLR 4.0 版。如果您知道 CLR(又名“公共语言运行时”)和“.NET 框架运行时”是同一个东西,那将是显而易见的,但大多数人实际上并不知道,除非他们已经是 .NET 程序员,所以……不完全是你没有猜测的错。

于 2014-12-13T06:14:27.507 回答