问题描述
免责声明-我对Python不熟悉.我是一位C#开发人员,他编写了一个应用程序来使用IronPython执行Python脚本(由他人编写).到目前为止,这些脚本只需要使用import math
,但是我们的一位用户要求该应用程序支持Numpy.
Disclaimer - I'm not familiar with Python. I'm a C# developer who has written an application to execute Python scripts (authored by others) using IronPython. These scripts have so far have only needed to use import math
, but one of our users has asked for the application to support for Numpy.
我已经在PC上安装了Numpy(使用"numpy-1.9.2-win32-superpack-python2.7.exe"文件),该文件在\ Lib \ site-packages下创建了一个numpy文件夹.我编写了两行Python脚本来测试Numpy的可访问性:-
I have installed Numpy on my PC (using the 'numpy-1.9.2-win32-superpack-python2.7.exe' file), which has created a numpy folder under \Lib\site-packages. I've written a two-line Python script to test that Numpy is accessible:-
import numpy as np
x = np.array([1,2])
我从C#中运行脚本:-
I run the script from within C#:-
var engine = Python.CreateEngine();
engine.SetSearchPaths(new Collection<string>(new[]
{
@"C:\Python27",
@"C:\Python27\DLLs",
@"C:\Python27\Lib",
@"C:\Python27\Lib\site-packages",
@"C:\Python27\Lib\site-packages\numpy",
@"C:\Python27\Lib\site-packages\numpy\core"
}));
var scope = engine.CreateScope();
var scriptSource = engine.CreateScriptSourceFromString(
_myPythonScript,
SourceCodeKind.Statements);
scriptSource.Execute(scope);
尽管设置了所有这些搜索路径,但最后一行抛出ImportException
:-
Despite setting all those search paths, the last line throws an ImportException
:-
请注意,此这样的文章是类似的,但是没有帮助-第一个答案提到了一个"mtrand.dll"文件,我似乎没有.
Note that this SO article is similar, but didn't help - the first answer mentions an 'mtrand.dll' file, which I don't seem to have.
我想念什么?
推荐答案
您遇到了IronPython的主要限制-它不支持CPython的C-API.因此,您需要使用pythonnet:
You hit the main limitation of IronPython - it does not support C-API of CPython. Hence you need to use pythonnet:
https://github.com/pythonnet/pythonnet
您可以尝试numpy的纯python实现:
You can try pure python implementation of numpy:
https://github.com/wadetb/tinynumpy
这篇关于IronPython无法运行导入numpy的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!