使用IronPython的通过C#.NET导入Python模块 [英] Import Python Module through C# .NET using IronPython
问题描述
我试图运行使用IronPython的通过C#.NET Python类,一对夫妇由Python类进口的模块有:
I am trying to run a Python class through C# .NET using IronPython, a couple of the Modules imported by the Python class are:
import collections
import nltk.classify.util
为了导入这些运行IronPython的时候,我使用的 GetSearchPath 在的ScriptEngine 的路径添加到Python库的位置,这样的集合:
In order to import these when running IronPython, I am using the GetSearchPath collection of the ScriptEngine to add the path to the location of the Python library, as such:
ICollection<string> paths = pyEngine.GetSearchPaths();
string dir = @"C:\Python27\Lib\";
paths.Add(dir);
string dir2 = @"C:\Python27\Lib\site-packages\nltk\classify\";
paths.Add(dir2);
pyEngine.SetSearchPaths(paths);
这似乎为在收藏模块运行正常,但不是< STRONG> nltk.classify.util ,然后调用ScriptEngine的Execute方法,当我收到以下错误:
This seems to run fine for the collections Module, but not the nltk.classify.util, and I get the following error when calling the Execute method of the ScriptEngine:
无模块命名NLTK。 classify.util
即使寿UTIL模块住在上面指定的路径。我把它的问题与进口在Python类中指定的方式做('。'分隔),只是不知道如何解决它。任何想法,我要去了?
Even tho the util module lives in the path specified above. I take it the issue has to do with the way the import is specified in the Python class ('.' delimited), just not sure how to solve it. Any ideas where am going wrong?
推荐答案
Python使用一个包名的结构搜索模块,所以如果你问为 nltk.classify.util
它会寻找 nltk\classify\util.py
在每个目录开始。搜索路径
Python uses the structure of a package name to search for a module, so if you ask for nltk.classify.util
it will look for nltk\classify\util.py
starting from each directory in the search path.
因此,在你的榜样,你想改变 DIR2
如下:
So in your example, you want to change dir2
as follows:
string dir2 = @"C:\Python27\Lib\site-packages";
这篇关于使用IronPython的通过C#.NET导入Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!