C# – How do I add a module to Iron Python?

How do I add a module to Iron Python?… here is a solution to the problem.

How do I add a module to Iron Python?

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

Once I run the program, it gives an exception

No module named mpl_toolkits.mplot3d

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

Note: graphcreater.py runs when executed using only Python.

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

Solution

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

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

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’ve installed the NetworkX package in my regular python venv.
I call import networkx from my IronPython embedded console in C# and get 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, it’s not entirely successful, but it shows that now I can import the installed packages.

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

Related Problems and Solutions