Skip to main content
 首页 » 编程设计

python之如何在 ironpython 2.7.5 中支持 numpy、scipy

2025年04月02日16hnrainll

我使用 https://store.enthought.com/repo/.iron/ 中的 numpy、scipy egg 包它在 ironpython 2.7.3 中表现良好。在窗口 7 中。 但是 ironpython 2.7.5 支持 pip 安装流程所以我升级到这个版本但是在 ironpython 中导入 numpy 是可以的。

我发现消息如下:

import numpy Traceback (most recent call last): File "", line 1, in File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- packages\numpy__init__.py", line 155, in File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- packages\numpy\core__init__.py", line 6, in File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- packages\numpy\core\multiarray.py", line 7, in ImportError: No module named NumpyDotNet

所以我将 IronPython-2.7.5\DLLs*.dll 复制到 IronPython-2.7.5 文件夹并解决了 NumpyDotNet 模块未找到的问题,但又出现了一个问题,我列出如下:

D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5>ipy 
IronPython 2.7.5 (2.7.5.0) on .NET 4.0.30319.34209 (32-bit) 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import numpy 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module> 
 File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- 
packages\numpy\__init__.py", line 155, in <module> 
 File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- 
packages\numpy\core\__init__.py", line 6, in <module> 
 File "D:\MyDoc\Training_Course\ISP\IronPython-2.7.5\IronPython-2.7.5\lib\site- 
packages\numpy\core\multiarray.py", line 6, in <module> 
IOError: System.IO.FileLoadException: Could not load file or assembly 'IronPytho 
n, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1' or one of 
 its dependencies. The located assembly's manifest definition does not match the 
 assembly reference. (Exception from HRESULT: 0x80131040) 
File name: 'IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c 
5b713576e1' 
 at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, Int 
Ptr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInst 
Count, ObjectHandleOnStack type) 
 at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 
typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] met 
hodInstantiationContext) 
 at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] ge 
nericTypeArguments, Type[] genericMethodArguments) 
 at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttrib 
uteRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Runtime 
Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilte 
rType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, 
RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters 
, Boolean& isVarArg) 
 at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decora 
tedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFi 
lterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecorate 
dTargetSecurityTransparent) 
 at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly asse 
mbly, RuntimeType caType) 
 at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, 
Boolean inherit) 
 at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, 
 Boolean inherit) 
 at Microsoft.Scripting.Utils.ReflectionUtils.GetCustomAttributes[T](Assembly 
assembly, Boolean inherit) 
 at IronPython.Runtime.Binding.PythonBinder.DomainManager_AssemblyLoaded(Objec 
t sender, AssemblyLoadedEventArgs e) 
 at System.EventHandler`1.Invoke(Object sender, TEventArgs e) 
 at Microsoft.Scripting.Runtime.ScriptDomainManager.LoadAssembly(Assembly asse 
mbly) 
 at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Assembly as 
sembly) 
 at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name 
) 
 at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object refe 
rence) 
 at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object[] re 
ferences) 
 at Microsoft.Scripting.Interpreter.ActionCallInstruction`2.Run(InterpretedFra 
me frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0 
, T1 arg1, T2 arg2, T3 arg3) 
 at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite s 
ite, T0 arg0, T1 arg1, T2 arg2) 
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame 
 frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0 
, T1 arg1, T2 arg2, T3 arg3) 
 at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedF 
rame frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) 
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) 
 at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) 
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleC 
ontext moduleContext, ScriptCode scriptCode, ModuleOptions options) 
 at IronPython.Runtime.PythonContext.CompileModule(String fileName, String mod 
uleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode) 
 at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, Stri 
ng name, String path) 
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, 
 String fullName, String str) 
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String 
 name, String fullName, List path, Func`5 defaultLoader) 
 at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String nam 
e, String fullName, List path) 
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globa 
ls, String modName, Boolean bottom, Int32 level) 
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Ob 
ject globals, Object locals, Object fromlist, Int32 level) 
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String f 
ullName, PythonTuple from, Int32 level) 
 at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, Str 
ing fullName, Int32 level) 
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame 
 frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) 
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) 
 at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) 
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleC 
ontext moduleContext, ScriptCode scriptCode, ModuleOptions options) 
 at IronPython.Runtime.PythonContext.CompileModule(String fileName, String mod 
uleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode) 
 at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, Stri 
ng name, String path) 
 at IronPython.Runtime.Importer.LoadPackageFromSource(CodeContext context, Str 
ing name, String path) 
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, 
 String fullName, String str) 
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String 
 name, String fullName, List path, Func`5 defaultLoader) 
 at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String nam 
e, String fullName, List path) 
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globa 
ls, String modName, Boolean bottom, Int32 level) 
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Ob 
ject globals, Object locals, Object fromlist, Int32 level) 
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String f 
ullName, PythonTuple from, Int32 level) 
 at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, Str 
ing fullName, Int32 level) 
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame 
 frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0) 
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) 
 at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope) 
 at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleC 
ontext moduleContext, ScriptCode scriptCode, ModuleOptions options) 
 at IronPython.Runtime.PythonContext.CompileModule(String fileName, String mod 
uleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode) 
 at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, Stri 
ng name, String path) 
 at IronPython.Runtime.Importer.LoadPackageFromSource(CodeContext context, Str 
ing name, String path) 
 at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, 
 String fullName, String str) 
 at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String 
 name, String fullName, List path, Func`5 defaultLoader) 
 at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String nam 
e, String fullName, List path) 
 at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String 
name) 
 at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globa 
ls, String modName, Boolean bottom, Int32 level) 
 at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Ob 
ject globals, Object locals, Object fromlist, Int32 level) 
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame 
 frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet 
](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) 
 at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String f 
ullName, PythonTuple from, Int32 level) 
 at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, Str 
ing fullName, Int32 level) 
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame 
 frame) 
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) 
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 a 
rg1) 
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 
 at IronPython.Compiler.PythonScriptCode.Run(Scope scope) 
 at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction 
>b__0() 
 
WRN: Assembly binding logging is turned OFF. 
To enable assembly bind failure logging, set the registry value [HKLM\Software\M 
icrosoft\Fusion!EnableLog] (DWORD) to 1. 
Note: There is some performance penalty associated with assembly bind failure lo 
gging. 
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus 
ion!EnableLog]. 

我曾经询问过 Enthought 支持,他们的回答是没有计划再升级它。他们建议我在这个论坛上寻求可能的支持 那么任何人都可以解决我遇到的问题吗?

提前感谢您。

请您参考如下方法:

几个月过去了,但是...

您需要按照错误消息中的描述启用绑定(bind)失败日志记录

To enable assembly bind failure logging, set the registry value [HKLM\Software\M icrosoft\Fusion!EnableLog] (DWORD) to 1.

然后您将能够看到程序集绑定(bind)失败的地方。在我的例子中,GAC 中有一个发布者策略将 IronPython 版本重定向到我的系统上不存在的东西。如果是这样,您需要在配置文件中将其注释掉。

例如在 policy.IronPython.config 中(启用程序集绑定(bind)失败日志记录后,您将能够看到确切的文件位置),将这两行注释掉,如下所示:

#         <bindingRedirect oldVersion="2.7.0.40" 
#                          newVersion="2.7.5.0"/> 

希望对您有所帮助。