【问题标题】:KeyNotFoundException on invoking IronPython调用 IronPython 时出现 KeyNotFoundException
【发布时间】:2011-11-21 14:55:01
【问题描述】:

我需要一个 C# 中的 MediaWiki 解析器。
(http://www.mediawiki.org/wiki/Alternative_parsers)
由于不存在,而且我真的想要一个 MediaWiki 解析器而不是其他的解析器,比如 WikiPlex,我正在研究使用另一种语言的一些现有解析器,并且能够成功调用 kiwi 解析器来实现这一点。但是,问题是它不能正确处理 Unicode/UTF-8。

现在,我想使用这个脚本:
http://www.connellybarnes.com/code/python/mw2html
并通过 IronPython 使用它。

我从这里使用 IronPython 2.7:
http://ironpython.net/

我下载了二进制 zip 文件(不是安装程序)。

然后我以本教程为基础:
http://www.codeproject.com/KB/cs/IronPythonMethod_CShap.aspx

这就是我所做的:

using System;
using IronPython.Hosting;


namespace IronPythonMethodInvokationDemo
{


    class Program
    {


        // http://stackoverflow.com/questions/6557494/referencing-python-import-assemblies-when-calling-from-ironpython-in-c
        static void Main(string[] args)
        {


            //Microsoft.Scripting.Hosting.ScriptEngine m_engine = Python.CreateEngine(DefaultEngineOptions());
            Microsoft.Scripting.Hosting.ScriptEngine m_engine = Python.CreateEngine();
            System.Collections.Generic.ICollection<string> paths = m_engine.GetSearchPaths();

            paths.Add(@"D:\username\Documents\Visual Studio 2010\Projects\IronPython\IronPython-Bin-2.7\IronPython-2.7\Lib"); // modules path
            paths.Add(@"D:\username\Documents\Visual Studio 2010\Projects\IronPython\IronPython-Bin-2.7\IronPython-2.7"); // ipy.exe path
            m_engine.SetSearchPaths(paths);


            //Step 1: 
            //Creating a new script runtime             
            //var ironPythonRuntime = Python.CreateRuntime();
            var ironPythonRuntime = m_engine.Runtime;

            try
            {
                //Step 2:
                //Load the Iron Python file/script into the memory
                //Should be resolve at runtime
                //dynamic loadIPython = ironPythonRuntime.UseFile("first.py");
                //ironPythonRuntime.GetBuiltinModule();
                dynamic loadIPython = ironPythonRuntime.UseFile("mw3html.py");

                //Step 3:
                //Invoke the method and print the result
                /*
                Console.WriteLine(
                                  string.Format("Addition result from IronPython method for {0} and {1} is {2}", 
                                  100,200, loadIPython.add(100, 200))
                                 );
                */

                Console.WriteLine( string.Format("Result for IronPython main method: {0}", loadIPython.main())  );
            } // End Try
            catch (System.IO.FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            } // End Catch
            Console.ReadKey(true);

        } // End Sub Main


    } // End Class Program


} // End Namespace IronPythonMethodInvokationDemo

如果我使用 first.py,那么一切正常。 现在,使用 mw2html.py,我得到了一个

IronPythonRuntime.UseFile("mw3html.py") 上的 KeyNotFoundException;为了 键“” ...

有人知道这是什么意思吗?

【问题讨论】:

  • 好像是import urllib和import urllib2造成的。。。貌似socket.py还有一些不在lib文件夹下。。。

标签: c# .net interop mediawiki ironpython


【解决方案1】:

继续您的评论:IronPython 中不存在 socket.py。相反,socket 模块是用 C# 编写的,并且位于 IronPython.Modules.dll 中。确保它也被您的项目引用,并且与 IronPython.dll 位于同一目录中。

【讨论】:

  • IronPython.Modules.dll被引用,执行前都被本地复制到bin文件夹中。
  • 你能从 ipy.exe 运行 mw2html.py 吗?这可能会为您提供有关失败的更多信息。
  • 是的,它运行在ipy.exe中,这很奇怪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多