Reputation: 2287
I've been trying to run python in C# .Net environment. It succeed to compile and run python script without importing any libraries. But, I need to import numpy in that python script ran in C# .Net in order to Execute it properly. This is my source code, without importing libraries and succeed:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using IronPython.Hosting;
using Microsoft.CSharp.RuntimeBinder;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the text you would like the script to print!");
var script =
"class MyClass:\r\n" +
" def __init__(self):\r\n" +
" pass\r\n" +
" def go(self, input):\r\n" +
" print('From dynamic python: ' + input)\r\n" +
" return input";
try
{
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
var ops = engine.Operations;
engine.Execute(script, scope);
var pythonType = scope.GetVariable("MyClass");
dynamic instance = ops.CreateInstance(pythonType);
var value = instance.go(input);
Console.WriteLine(value);
}
catch (Exception ex)
{
Console.WriteLine("Oops! There was an exception" +
" while running the script: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
but, when I tried to import numpy :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using IronPython.Hosting;
using Microsoft.CSharp.RuntimeBinder;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the text you would like the script to print!");
var script =
"import numpy" //I added this module
"class MyClass:\r\n" +
" def __init__(self):\r\n" +
" pass\r\n" +
" def go(self, input):\r\n" +
" print('From dynamic python: ' + input)\r\n" +
" return input";
try
{
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
var ops = engine.Operations;
engine.Execute(script, scope);
var pythonType = scope.GetVariable("MyClass");
dynamic instance = ops.CreateInstance(pythonType);
var value = instance.go(input);
Console.WriteLine(value);
}
catch (Exception ex)
{
Console.WriteLine("Oops! There was an exception" +
" while running the script: " + ex.Message);
}
Console.WriteLine("Press enter to exit...");
Console.ReadLine();
It gave me error :
No module named numpy
How to solve this, thank you.
Upvotes: 1
Views: 14413
Reputation: 1833
IronPython is a Python implementation on top of the .Net runtime. It is only able to import and use modules that are written in pure Python or are part of the standardlib distributed with IronPython itself. As far as I know, numpy is not one of those, it's a C-based extension.
I expect you installed some other C-based Python implementation (regular CPython, Anaconda or something else) then added numpy to it, and you are trying to call that from IronPython. That is just not possible.
The best you can do is to save your Python script in a .py file, then pass it as parameter to python.exe and retrieve the results. You can likely do that with regular .Net, look for ways of running any exe from C#.
Upvotes: 2