How to add modules to Iron Python?

人盡茶涼 提交于 2019-12-12 10:07:40

问题


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,

No module named mpl_toolkits.mplot3d

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

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

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# Code :

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

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

回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/42343676/how-to-add-modules-to-iron-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!