0

I am very new to this and require some help. I am trying to call something basic as per the section below using C# with the IronPython nuget.

Below is what I am trying:

 Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine(); 
 Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope(); 



py.Execute("import numpy as np incomes = np.random.normal(27000, 15000, 10000) x = np.mean(incomes)", s);

I keep receiving the following error: An exception of type 'Microsoft.Scripting.SyntaxErrorException' occurred in Microsoft.Scripting.dll but was not handled in user code

Any help would be appreciated thank you

1
  • 1
    you can use pyinstaller instead Commented Feb 20, 2019 at 11:29

2 Answers 2

2

Be aware of indentation when working with Python. This works:

using IronPython.Hosting;
namespace PythonFromCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine py = Python.CreateEngine();
            Microsoft.Scripting.Hosting.ScriptScope s = py.CreateScope();

            // add paths to where your libs are installed
            var libs = new[] { @"c:\path\to\lib1", @"c:\path\to\lib2" };
            py.SetSearchPaths(libs);

            py.Execute(
@"import numpy as np 
incomes = np.random.normal(27000, 15000, 10000) 
x = np.mean(incomes)"
            , s);
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this is great advise. I still get the following error after:An exception of type 'IronPython.Runtime.Exceptions.ImportException' occurred in Microsoft.Dynamic.dll but was not handled in user code
Try adding paths to where numpy is installed. I updated my answer with an example on how to do that :)
This has been so helpful thank you. I have installed and added per your suggestion. I have made sure everything is up to date and installed correctly, but I get this error. unexpected token 'append' is there an issue with the way I set it up?
@JacobO'Brien Not sure why that is a problem, but I'd suggest you develop your python script in an other environment to start with and make sure that the script works as intended. Then you let your C# program open the file containing the script, instead of having it as part of the C# code. Indentation and line breaks are very important when it comes to python so make sure to get it right. I'd say that's much easier if you keep your script in a separate file rather than stored in a C# string.
1

Your Python syntax is incorrect. Insert line breaks (\n)

py.Execute("import numpy as np\nincomes = np.random.normal(27000, 15000, 10000)\nx = np.mean(incomes)", s);

2 Comments

You, sir, are a legend! I have done that, but now get the following :An exception of type 'IronPython.Runtime.Exceptions.ImportException' occurred in Microsoft.Dynamic.dll but was not handled in user code
You need to install numpy, try doing so using pip or a similar tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.