Calling Python from C#

Calling Python from C#

Follow the instructions:

  1. Install IronPython and get SymPy
  2. Add these resources to your project
    IronPython.dll
    IronPython.Modules.dll
    Microsoft.Dynamic.dll
    Microsoft.Scripting.dll
  3. C# code as follows:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;

namespace TestPy
{
    class Program
    {
        static void Main(string[] args)
        {
            var options = new Dictionary<string, object>();
            options["Frames"] = true;
            options["FullFrames"] = true;
            ScriptEngine engine = Python.CreateEngine(options);
            var paths = engine.GetSearchPaths();
            paths.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
            paths.Add(@"C:\dev\sympy\sympy-0.7.6"); //also the following is working
            //paths.Add(@"C:\python34_64bit\Lib\site-packages");
            engine.SetSearchPaths(paths);

            // expression to simplify
            var expr = "0 + 1 * 1 * (x - 2) / (1 - 2) * (x - 3) / (1 - 3) * (x - 4) / (1 - 4) + 8 * 1 * (x - 1) / (2 - 1) * (x - 3) / (2 - 3) * (x - 4) / (2 - 4) + 27 * 1 * (x - 1) / (3 - 1) * (x - 2) / (3 - 2) * (x - 4) / (3 - 4) + 64 * 1 * (x - 1) / (4 - 1) * (x - 2) / (4 - 2) * (x - 3) / (4 - 3)";

            string myexpr = Console.ReadLine();

            if (myexpr !=null && myexpr.Length>1)
            {
                expr = myexpr;
            }

            var scope = engine.CreateScope();

            var script = engine.CreateScriptSourceFromString(@"
import codecs
def my_unicode_escape_decode(x):
    return x
codecs.unicode_escape_decode = my_unicode_escape_decode

from sympy import *
import clr
from System import String

expr = simplify('" + expr + @"')
result = clr.Convert(expr, String)
");

            script.Execute(scope);
            Console.WriteLine(scope.GetVariable("result"));
        }
    }
}

See a test below
sympy
gamma

4 thoughts on “Calling Python from C#

  1. Hey there! Thank you so so much for this post! I finally found someone who was able to implement sympy with IronPython!! If you don’t mind i have few questions. So I tried to follow your instructions but it didn’t work with me:( I’m pretty sure it has to do with the paths. So as i can see, you downloaded ironpython separately from their website, you didn’t download it from NuGet package provided in VS like i did. And I’m restricted to only using the ironpython from the VS because in the project I’m working on, It will be complicated to load external things in the cloud and will be harder to navigate any errors if any were found so I prefer using the Ironpython locally. With that being said, my first question is:
    1) Do I need the first path.add function? Since i have ironpython in my solution i assume the it will be easily navigated to, am i right? if not I also tried putting the path of the Ironpython package in my solution in the first path function as shown here (paths.Add(@”C:\Users\guestuser\Desktop\zein\ConsoleApp1\packages\IronPython.2.7.9\lib”) , where zein is the folder name where my project “ConsoleApp1” resides) but it didn’t work.
    My second question is :
    2) It’s about sympy, I see that you’re using sympy version 0.6.7 which is not the newest one (1.4 is the newest). I wonder is there any reason for that?
    Third question:
    3) In the first step you mentioned , you said “get sympy”, what do you mean by that? Downloading the zip file? because that’s exactly what I’m doing but I want to make and i also want to know where to place it. should i put it inside my project or anywhere is fine?
    Fourth question:
    4) The second paths.Add function is to add the sympy path. I have mine as this>>>> paths.Add(@”C: \Users\guestuser\Downloads\sympy – sympy – 1.4\sympy – sympy – 1.4\sympy”) where in the last sympy i have the init.py file that contains all the imports, am i doing it right?? Because i can’t tell how deep you went inside the sympy folder.
    I’m sorry for all these questions, i know they might look dumb to you. But as you can tell, I don’t fully understand the paths side of the code. I really wish you can help me with this, I have been trying to implement sympy to the .net framework for awhile, and i tried so many ways(brython, process, etc) then I looked @ ironpython but i couldn’t make it work and I thought it’s impossible until I saw your amazing article!! Thank you so much in advance!! Appreciate all the help<3

    Like

    • Also can you provide me with your source code sample files with everything that is needed for it work? I tried again and played with the paths a little but i wasn’t able to make it work:c

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s