ImportError with IronPython in C#

本文详细介绍了如何解决在C#环境下使用IronPython时遇到的导入错误问题,特别是当尝试使用xmlrpclib模块时出现的ImportError:未找到模块。通过查找IronPython的搜索路径、保存这些路径并在C#代码中加入所有路径,可以确保IronPython在尝试导入缺失库时进行正确的搜索。

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.

转载于:https://www.cnblogs.com/mschen/p/5349555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值