Pyparsing在C#代码中的使用
我正试图在IronPython的帮助下执行python脚本,该脚本在C#中使用pyparsing。但是当我尝试运行脚本时,我得到了一个ImportException,即没有名为pyparsing的模块。我试图为目录添加一条路径,但仍然无法以正确的方式运行它 这是C代码:Pyparsing在C#代码中的使用,c#,ironpython,pyparsing,C#,Ironpython,Pyparsing,我正试图在IronPython的帮助下执行python脚本,该脚本在C#中使用pyparsing。但是当我尝试运行脚本时,我得到了一个ImportException,即没有名为pyparsing的模块。我试图为目录添加一条路径,但仍然无法以正确的方式运行它 这是C代码: string ExecutePythonScript(字符串路径,字符串文本) { ScriptEngine=Python.CreateEngine(); ScriptScope scope=engine.CreateScope
string ExecutePythonScript(字符串路径,字符串文本)
{
ScriptEngine=Python.CreateEngine();
ScriptScope scope=engine.CreateScope();
string dir=System.IO.Path.GetDirectoryName(“pyparsing-1.5.7”);
ICollection path=engine.getSearchPath();
如果(!String.IsNullOrEmpty(dir))
{
添加路径(dir);
}
其他的
{
Add(Environment.CurrentDirectory);
}
引擎。设置搜索路径(路径);
scope.SetVariable(“文本”,文本);
engine.ExecuteFile(路径、范围);
返回scope.GetVariable(“结果”);
}
当然,在编写python脚本的过程中,我导入了pyparsing。多亏了我的朋友,我发现了问题所在
string ExecutePythonScript(string path, string text)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ICollection<string> Paths = engine.GetSearchPaths();
Paths.Add(".");
Paths.Add("D:\\DevTools\\IronPython 2.7\\Lib");
Paths.Add("D:\\DevTools\\IronPython 2.7\\DLLs");
Paths.Add("D:\\DevTools\\IronPython 2.7");
Paths.Add("D:\\DevTools\\IronPython 2.7\\lib\\site-packages");
engine.SetSearchPaths(Paths);
scope.SetVariable("text", text);
engine.ExecuteFile(path, scope);
(...)
string ExecutePythonScript(字符串路径,字符串文本)
{
ScriptEngine=Python.CreateEngine();
ScriptScope scope=engine.CreateScope();
ICollection path=engine.getSearchPath();
路径。添加(“.”);
添加(“D:\\DevTools\\ironpython2.7\\Lib”);
添加(“D:\\DevTools\\ironpython2.7\\DLLs”);
添加(“D:\\DevTools\\ironpython2.7”);
添加(“D:\\DevTools\\ironpython2.7\\lib\\site包”);
引擎。设置搜索路径(路径);
scope.SetVariable(“文本”,文本);
engine.ExecuteFile(路径、范围);
(...)
至少没有造成这种例外。多亏了我的朋友,我才发现了问题所在
string ExecutePythonScript(string path, string text)
{
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
ICollection<string> Paths = engine.GetSearchPaths();
Paths.Add(".");
Paths.Add("D:\\DevTools\\IronPython 2.7\\Lib");
Paths.Add("D:\\DevTools\\IronPython 2.7\\DLLs");
Paths.Add("D:\\DevTools\\IronPython 2.7");
Paths.Add("D:\\DevTools\\IronPython 2.7\\lib\\site-packages");
engine.SetSearchPaths(Paths);
scope.SetVariable("text", text);
engine.ExecuteFile(path, scope);
(...)
string ExecutePythonScript(字符串路径,字符串文本)
{
ScriptEngine=Python.CreateEngine();
ScriptScope scope=engine.CreateScope();
ICollection path=engine.getSearchPath();
路径。添加(“.”);
添加(“D:\\DevTools\\ironpython2.7\\Lib”);
添加(“D:\\DevTools\\ironpython2.7\\DLLs”);
添加(“D:\\DevTools\\ironpython2.7”);
添加(“D:\\DevTools\\ironpython2.7\\lib\\site包”);
引擎。设置搜索路径(路径);
scope.SetVariable(“文本”,文本);
engine.ExecuteFile(路径、范围);
(...)
至少不会产生这种异常。我可以问一下,为什么选择这种解决方案(C#->IronPython)而不是使用F#编写的语法分析器,使用FParsec或flex/fyacc吗?(我是.net的老手,学习python,尤其是语法分析))很抱歉回答了这么长时间。我们的团队刚刚尝试为我们学院的人工智能实验室创建一些东西,每个人都采用了他们最喜欢的技术。这是我们试图将其全部连接到一个项目时遇到的问题。我们当时只是没有考虑。我能问你为什么选择此解决方案(C#->IronPython)吗与使用F#编写的语法分析器(使用FParsec或flex/fyacc)不同(我是.net的老手,学习python,尤其是语法分析))很抱歉回应了这么长时间。我们的团队刚刚尝试为我们学院的人工智能实验室创造一些东西,每个人都采用了他们最喜欢的技术。当我们试图将所有这些都连接到一个项目时,这就是问题所在。当时我们只是没有考虑这个问题。