本文介绍了从C#(与SciPy的)IronPython的调用失败,ImportException:[否模块名为mtrand"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python库我试图通过IronPython的(V2.7 RC1 [2.7.0.30])从C#应用程序调用使用。库使用numpy的和SciPy的相当广泛,其中确实工作与利用这样的命令行IPY运行时:

I have a python library I am trying to use via IronPython (v2.7 RC1 [2.7.0.30]) invocation from C# application. The library uses NumPy and SciPy quite extensively, which does work with SciPy and NumPy for .NET when ran using ipy from command line like this:

ipy.exe -X:Frames file_from_lib_importing_numpy.py

然而,当我使用下面的代码从C#调用IronPython的,异常被抛出:

However, when I invoke IronPython from C# using the code below, an exception is thrown:

ImportException
"No module named mtrand"
   at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
   at IronPython.Runtime.Operations.PythonOps.ImportStar(CodeContext context, String fullName, Int32 level)
   at Microsoft.Scripting.Interpreter.ActionCallInstruction3.Run(InterpretedFrame frame)
   ...
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path)
   at Microsoft.Scripting.Hosting.ScriptRuntime.ExecuteFile(String path)
   at Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path)
   ...

这调用IronPython的是如下的C#代码(它的一部分):

The C# code (a part of it) that invokes IronPython is as follows:

    ScriptEngine _engine;

    var opts = new Dictionary<string, object>();
    opts["Frames"] = ScriptingRuntimeHelpers.True;
    _engine = Python.CreateEngine(opts);

    var sp = _engine.GetSearchPaths();
    sp.Add(@"c:\Program Files\IronPython 2.7");
    sp.Add(@"c:\Program Files\IronPython 2.7\DLLs");
    sp.Add(@"c:\Program Files\IronPython 2.7\Lib");
    sp.Add(@"c:\Program Files\IronPython 2.7\Lib\site-packages");
    sp.Add(_path);
    _engine.SetSearchPaths(sp);

    var _runtime = _engine.Runtime;
    var scope = _runtime.ExecuteFile(Path.Combine(_path, "mytest.py"));

有关测试目的,我执行了以下文件'mytest.py':

For testing purposes I am executing the following file 'mytest.py':

    import sys
    sys.path.append(r'c:\Program Files\IronPython 2.7')
    sys.path.append(r'c:\Program Files\IronPython 2.7\DLLs')
    sys.path.append(r'c:\Program Files\IronPython 2.7\Lib')
    sys.path.append(r'c:\Program Files\IronPython 2.7\Lib\site-packages')

    import os, os.path
    cd = os.path.dirname(__file__)
    if not cd in sys.path:
        sys.path.append(os.path.dirname(__file__))

    import numpy as np
    print 'OK'

    x = np.array([1,2])
    print x

上线12'进口numpy的是NP这将失败。
的问题是,该文件 __ __初始化。PY 在IronPython的2.7\Lib\site-packages\\\
umpy\random\包含以下行

Which fails on the line 12 'import numpy as np'. The problem is that the file __init__.py in IronPython 2.7\Lib\site-packages\numpy\random\ contains the following line

from mtrand import *

而失败。请注意,mtrand是不是一个模块,它是一个的目录的。
我想不出别的我可以尝试,使这项工作,所以我非常感谢您的任何帮助。
非常感谢你。

which fails. Note that the mtrand is not a module, it is a directory.I can not think of anything else I can try to make this work, so I would very much appreciate any help from you.Thank you very much.

推荐答案

不是最好的soultion,但它的工作对我来说:

Not the best soultion, but its works for me:

import sys
sys.path.append(r'c:\Program Files (x86)\IronPython 2.7')
sys.path.append(r'c:\Program Files (x86)\IronPython 2.7\DLLs')
sys.path.append(r'c:\Program Files (x86)\IronPython 2.7\Lib')
sys.path.append(r'c:\Program Files (x86)\IronPython 2.7\Lib\site-packages')

import clr
clr.AddReference('mtrand.dll')

import numpy
import scipy

print numpy.__version__
print scipy.__version__

我希望它能帮助。

这篇关于从C#(与SciPy的)IronPython的调用失败,ImportException:[否模块名为mtrand&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:07