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.