如何向Iron Python添加模块? [英] How to add modules to Iron Python?

查看:102
本文介绍了如何向Iron Python添加模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用C#Visual Studio执行以下Python代码(graphcreater.py).我通过NuGet软件包管理器添加了IronPyton 2.7.7和IronPython.StdLib 2.7.7.

I have been trying to execute following Python code (graphcreater.py) using C# Visual Studio. I added IronPyton 2.7.7 and IronPython.StdLib 2.7.7 via NuGet package manager.

一旦我运行程序,它就会给出一个异常提示,

Once I run the program it gives an exception saying that,

没有名为mpl_toolkits.mplot3d的模块

No module named mpl_toolkits.mplot3d

我需要弄清楚如何在Python代码(graphcreater.py)中正确导入mpl_toolkits模块.

I need to figure out how to import mpl_toolkits module in Python code (graphcreater.py) correctly.

注意:仅使用Python执行时,graphcreater.py才会运行.

NOTE: The graphcreater.py is running when executed using Python only.

Python代码(graphcreater.py):

Python Code (graphcreater.py):

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()

xs = np.array([ 0,1,2,2,1,1,0]);
ys = np.array([ 0,0,0,2,2,3,3]);
zs = np.array([3 ,0, -1, 6, 2, 1,4]);

ax=fig.add_subplot(1,1,1, projection='3d')
ax.grid(True)

ax.plot_trisurf(xs, ys, zs,cmap=cm.coolwarm,linewidth=0.2, antialiased=True)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# Add a color bar which maps values to colors
# fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()

C#代码:

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace graphCreator
{
  class Program
  {
    static void Main(string[] args)
    {
        ScriptEngine engine = Python.CreateEngine();
        engine.ExecuteFile(@"graphcreater.py");
    }
  }
}

推荐答案

阅读后,我想有一个解决方案:

I think I have a solution after reading: http://www.needfulsoftware.com/IronPython/IronPythonCS2

我们可以为要使用的库设置搜索路径.例如,我将搜索路径修改如下:

We can set search paths for the libraries we want to use. For example I modified my search path as following:

ICollection<string> searchPaths = engine.GetSearchPaths();
searchPaths.Add("J:\\Python\\test2\\venv\\Lib");
searchPaths.Add("J:\\Python\\test2\\venv\\Lib\\site-packages");
engine.SetSearchPaths(searchPaths);

我已经在常规的python venv中安装了NetworkX软件包.我在C#中从IronPython嵌入式控制台调用了 import networkx ,并收到以下错误:

I had installed NetworkX package in my regualar python venv. I called import networkx from my IronPython embedded console in C# and got the following error:

>>> import networkx
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\networkx\__init__.py", line 128, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\networkx\drawing\__init__.py", line 6, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\networkx\drawing\nx_pydot.py", line 27, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\__init__.py", line 77, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\_vendor\packaging\requirements.py", line 9, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\extern\__init__.py", line 43, in load_module
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\_vendor\pyparsing.py", line 4715, in <module>
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\_vendor\pyparsing.py", line 1261, in setParseAction
  File "J:\Python\test2\venv\Lib\site-packages\pkg_resources\_vendor\pyparsing.py", line 1043, in _trim_arity
IndexError: index out of range: -1

因此,这并不是完全成功,但是它表明,如果安装了软件包,现在我可以导入它们.

So, this is not complete success, but it shows that now I can import packages if they are installed.

问题在于,并非所有软件包都与IronPython兼容,正如其网站中提到的那样.因此,我建议的最佳解决方案是将IronPython(ipy.exe)安装在文件夹中,然后安装所需的支持程序包,然后可以将C#中的搜索路径更新为ipy的 site-packages 您已安装.

The issue is that not all packages are compatible with IronPython as it is mentioned in their website. So the best solution I can suggest is to install IronPython (ipy.exe) in a folder, and then install supported packages you want, then you can update search path in C# to site-packages of the ipy you installed.

这篇关于如何向Iron Python添加模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆