zoukankan      html  css  js  c++  java
  • ImportError with IronPython in C#

    I was using IronPython to execute python code inside my C# implementation lately, and I encountered this error when trying to use xmlrpclib:

      
        ImportError: No module named xmlrpclib.

    It was really frustrating because if I try the same in IronPython console, it works fine. It turned out that this was a problem with search paths. When called via C# code, IronPython does not search for missing libraries unless you provide it a path to search for. Here’s how I solved this issue:

    1. First, find out the search paths that IronPython is using in console mode:
    import sys
    print sys.path

    This will print all the search paths. Save these paths somewhere.

    1. Now include all these search paths inside your C# code:
    ScriptEngine Engine = Python.CreateEngine();
    ICollection<string> Paths = Engine.GetSearchPaths();
    Paths.Add("<Path>");
    Engine.SetSearchPaths(Paths);

    Replace <Path> with the path you saved earlier. Now IronPython will search all these pathsbefore failing with an ImportError.

  • 相关阅读:
    1028 人口普查 (20分)
    1027 打印沙漏 (20分)
    1026 程序运行时间 (15分)
    1025 反转链表 (25分)
    1024 科学计数法 (20分)
    1023 组个最小数 (20分)
    1022 D进制的A+B (20分)
    1021 个位数统计 (15分)
    1020 月饼 (25分)
    1019 数字黑洞 (20分)
  • 原文地址:https://www.cnblogs.com/mschen/p/5349555.html
Copyright © 2011-2022 走看看