首页 > 编程语言 > 详细

ImportError with IronPython in C#

时间:2016-04-03 13:01:02      阅读:367      评论:0      收藏:0      [点我收藏+]

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.

ImportError with IronPython in C#

原文:http://www.cnblogs.com/mschen/p/5349555.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!