我对此很陌生,需要一些帮助。我正在尝试使用IronPython nuget将C#称为以下部分中的基本内容。
以下是我正在尝试的内容:
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);
我一直收到以下错误: Microsoft.Scripting.dll中发生了'Microsoft.Scripting.SyntaxErrorException类型的异常,但未在用户代码中处理
任何帮助,谢谢您
答案 0 :(得分:2)
使用Python时要注意缩进。这有效:
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);
}
}
}
答案 1 :(得分:1)
您的Python语法不正确。插入换行符(\n
)
py.Execute("import numpy as np\nincomes = np.random.normal(27000, 15000, 10000)\nx = np.mean(incomes)", s);