ironpython使用numpy,IronPython无法运行导入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.

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])

I run the script from within C#:-

var engine = Python.CreateEngine();

engine.SetSearchPaths(new Collection(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);

Despite setting all those search paths, the last line throws an ImportException:-

cannot import multiarray from numpy.core

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.

What am I missing?

解决方案

You hit the main limitation of IronPython - it does not support C-API of CPython. Hence you need to use pythonnet:

You can try pure python implementation of numpy:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值