[英]“No module named fcntl” when py script run from c# but works from windows command line
[英]No module named fcntl
我正在嘗試使用 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 模塊
fcntl並不是真正的 Windows 原生(平台:Unix)所以你可能不走運, 下面的 StackOverflow 線程可能(也可能沒有)有幫助......
當我遇到這個問題時,問題原來是我在搜索路徑中只有我的 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)
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
我認為到目前為止,您最簡單的解決方案是切換到 CPython。 我認為它不會比您現有的解決方案集成度低,而且您可以避免所有模塊丟失的麻煩。
遇到了完全相同的問題,直到我完全像這樣訂購導入和 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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.