C# 应用程序中的 IronPython - SyntaxErrorException: 'invalid syntax'
IronPython in a C# application - SyntaxErrorException: 'invalid syntax'
我正在尝试在 C# WPF 应用程序中使用 IronPython 执行一些 python。下面的代码在不删除 'import os' 行的情况下工作。通过该行,我得到一个 SyntaxErrorException: 'invalid syntax' 错误。我认为这是正确的语法?
public MainWindow()
{
InitializeComponent();
ScriptEngine pythonEngine = Python.CreateEngine();
var paths = pythonEngine.GetSearchPaths();
paths.Add(@"C:\Python38\Lib");
pythonEngine.SetSearchPaths(paths);
ScriptScope scope = pythonEngine.CreateScope();
scope.SetVariable("App", this);
ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(@"
import os
f = open('demofile4.txt', 'a')
f.write('Now the file has more content!')
f.close()");
pythonScript.Execute(scope);
}
我现在有这个工作。 IronPython 与 Python 3.8 不兼容。来自网站:
IronPython 3.4 uses Python 3.4 syntax and standard libraries
我下载了 Python 3.4.10 并在我的代码中更改了:
paths.Add(@"C:\Python38\Lib");
收件人:
paths.Add(@"C:\Python-3.4.10\Lib");
我正在尝试在 C# WPF 应用程序中使用 IronPython 执行一些 python。下面的代码在不删除 'import os' 行的情况下工作。通过该行,我得到一个 SyntaxErrorException: 'invalid syntax' 错误。我认为这是正确的语法?
public MainWindow()
{
InitializeComponent();
ScriptEngine pythonEngine = Python.CreateEngine();
var paths = pythonEngine.GetSearchPaths();
paths.Add(@"C:\Python38\Lib");
pythonEngine.SetSearchPaths(paths);
ScriptScope scope = pythonEngine.CreateScope();
scope.SetVariable("App", this);
ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(@"
import os
f = open('demofile4.txt', 'a')
f.write('Now the file has more content!')
f.close()");
pythonScript.Execute(scope);
}
我现在有这个工作。 IronPython 与 Python 3.8 不兼容。来自网站:
IronPython 3.4 uses Python 3.4 syntax and standard libraries
我下载了 Python 3.4.10 并在我的代码中更改了:
paths.Add(@"C:\Python38\Lib");
收件人:
paths.Add(@"C:\Python-3.4.10\Lib");