Pet*_*ter 14 c# ironpython numpy scipy python.net
我有一个python库,我试图通过来自C#应用程序的IronPython(v2.7 RC1 [2.7.0.30])调用来使用.该库非常广泛地使用NumPy和SciPy,当从命令行使用ipy运行时,它可以与SciPy和NumPy for .NET一起使用,如下所示:
ipy.exe -X:Frames file_from_lib_importing_numpy.py
Run Code Online (Sandbox Code Playgroud)
但是,当我使用下面的代码从C#调用IronPython时,会抛出异常:
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)
...
Run Code Online (Sandbox Code Playgroud)
调用IronPython的C#代码(它的一部分)如下:
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"));
Run Code Online (Sandbox Code Playgroud)
出于测试目的,我正在执行以下文件'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
Run Code Online (Sandbox Code Playgroud)
哪个在第12行导入numpy失败为np'.问题是__init__.py
IronPython 2.7\Lib\site-packages \numpy\random \中的文件包含以下行
from mtrand import *
Run Code Online (Sandbox Code Playgroud)
哪个失败了.请注意,mtrand不是模块,而是目录.我想不出别的什么我可以尝试使这项工作,所以我非常感谢你的任何帮助.非常感谢你.
小智 12
不是最好的灵魂,但它的作用对我来说:
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__
Run Code Online (Sandbox Code Playgroud)
我希望它有所帮助.
归档时间: |
|
查看次数: |
7729 次 |
最近记录: |