ImportError with IronPython in C#
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
- 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.
- 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#的主要内容,如果未能解决你的问题,请参考以下文章
ImportError: DLL load failed with error code -1073741795
python-social-auth with Django: ImportError: No module named 'social_django'
ImportError: DLL load failed with error code -1073741795
ImportError: cannot import name 'dnn_superres' for python example of super resolution with opencv