我正在尝试使用 C# 和 IronPython nuget 调用 python
I am trying to call python using C# with the IronPython nuget
我对此很陌生,需要一些帮助。我正在尝试根据下面的部分使用 C# 和 IronPython nuget 来调用一些基本的东西。
下面是我正在尝试的:
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.SyntaxErrorException' 类型的异常发生在 Microsoft.Scripting.dll 但未在用户代码中处理
任何帮助将不胜感激谢谢
您的 Python 语法不正确。插入换行符 (\n
)
py.Execute("import numpy as np\nincomes = np.random.normal(27000, 15000, 10000)\nx = np.mean(incomes)", s);
使用 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);
}
}
}
我对此很陌生,需要一些帮助。我正在尝试根据下面的部分使用 C# 和 IronPython nuget 来调用一些基本的东西。
下面是我正在尝试的:
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.SyntaxErrorException' 类型的异常发生在 Microsoft.Scripting.dll 但未在用户代码中处理
任何帮助将不胜感激谢谢
您的 Python 语法不正确。插入换行符 (\n
)
py.Execute("import numpy as np\nincomes = np.random.normal(27000, 15000, 10000)\nx = np.mean(incomes)", s);
使用 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);
}
}
}