2

我正在尝试使用 IronPython 2.7 在 .NET 4.0 上使用 IronPython 执行此方法。我正在使用 Windows 7

import os
import re
import nltk
import urllib
import xapian
import sys

def getData(url):
        try:
         html = urllib.urlopen(url)
         text = html.read()
         html.close()
        except:
            return "error"
        try:
            return nltk.clean_html(text) #takes the tokens
        except:
            return text

C#代码:

public static object Execute()
        {
            string scriptPath = "Calculator.py";
            ScriptEngine engine = Python.CreateEngine();
            engine.SetSearchPaths(new string[] { "c:\\Python26\\lib","c:\\Python26\\lib\\site-packages",
                "C:\\IronPython-2.7\\Lib\\site-packages","C:\\IronPython-2.7\\Lib"});
            ScriptSource source = engine.CreateScriptSourceFromFile(scriptPath);
             ScriptScope scope = engine.CreateScope();
        ObjectOperations op = engine.Operations;
        source.Execute(scope);

        dynamic Calculator = scope.GetVariable("Calculator");
        dynamic calc = Calculator();

        return calc.getData("http://www.wowebook.com/dot-net/ironpython-in-action.html");



        }

有人可以告诉我我做错了什么吗?我一直说我没有 fcntl 模块

4

5 回答 5

3

fcntl并不是真正的 Windows 原生(平台:Unix)所以你可能不走运,下面的 StackOverflow 线程可能(或可能没有)有帮助......

于 2011-04-04T19:28:52.193 回答
1

当我遇到这个问题时,问题原来是我的搜索路径中只有我的 CPython 库(我之前在 CPython 中安装了 NLTK)而不是 IronPython 库。

在我的 C# 代码中,我现在有类似的东西

 engine.SetSearchPaths(new string[] {"C:\\Program Files\\IronPython 2.7\\Lib"
                                    ,"C:\\Python27\\Lib"
                                    ,"C:\\Python27\\Lib\\site-packages"
                                    });

当我为这个确切的问题挠头时,我注意到我不小心输入了 2.7.1 作为我的 IronPython 路径,即。一个不存在的目录。哦,我刚刚注意到 OP 在他们的源中有一个类似的搜索路径条目,也许也可能是搜索路径的顺序?

对处于类似位置的人的有用线索:我注意到我的 NLTK 使用代码在从 ipy.exe 加载时工作得很好,所以不是这样的可移植性问题......(并且 NLTK 源不包含字符串 fcntl 任何地方)

于 2013-04-19T16:01:48.473 回答
0

我认为到目前为止,您最简单的解决方案是切换到 CPython。我认为它的集成度不会低于您现有的解决方案,并且您可以避免缺少模块的所有麻烦。

于 2011-04-04T19:55:42.457 回答
0
import sys
sys.path.append("X:\Python27x64")
sys.path.append("X:\Python27x64\DLLs")
sys.path.append("X:\Python27x64\Lib")
sys.path.append("X:\Python27x64\Lib\site-packages")
sys.platform = "win32"
import nltk
于 2013-05-30T17:31:43.153 回答
0

遇到完全相同的问题,直到我像这样订购导入和 sys.path.append 之前没有任何效果:

sys.path.append("C:\\Program Files\\IronPython 2.7\\Lib") sys.path.append("C:\\Program Files\\IronPython 2.7\\Lib\\site-packages") sys.path.append("C:\\Python27\\Lib") sys.path.append("C:\\Python27\\Lib\\site-packages")

于 2020-04-09T12:58:09.630 回答