2011-04-04 139 views
2

我想用IronPython 2.7在.NET 4.0上使用IronPython執行此方法。我使用Windows 7沒有模塊名爲fcntl

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模塊

回答

0

我想到目前爲止,您最簡單的解決方案是切換到CPython。我不認爲它會比現有的解決方案更少集成,你會避免所有缺少模塊的麻煩。

+0

謝謝,我會試試看。 – michelle 2011-04-04 20:02:35

3

的fcntl isn't really一個Windows本地(平臺:Unix的),所以你可能是出於運氣,the following StackOverflow thread可能(也可能不)是有幫助的...

+0

是的,我認爲一樣多。你能指出fcntl會在哪裏使用嗎?因爲當我執行這個方法使用Python 26時,它運行良好 – michelle 2011-04-04 19:31:53

+0

當你說python 26你的意思是CPython嗎? – 2011-04-04 19:40:02

+0

我不確定。我用IDLE GUI Python 2.6編譯它。我有點困惑,因爲我一直試圖刪除代碼的幾個部分,看看fcntl信號是如何發出的,但我運氣不好。 – michelle 2011-04-04 19:51:06

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在它們的源代碼中有一個類似的搜索路徑條目,也許也可能是搜索路徑的順序?

對於處於類似位置的人有用的線索:我注意到,從ipy.exe加載它時,我的NLTK使用代碼工作得很好,因此不是像這樣的可移植性問題...(並且NLTK源不包含字符串fcntl )

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 
相關問題