最近在開發微信公眾號,有一個自定義消息回復的需求
比如用戶:麻煩幫我查詢一下北京的天氣? 系統回復:北京天氣,晴,8-13℃。。。 這時候需要根據關鍵字【北京】【天氣】,分詞匹配需要執行的操作,然后去調用天氣接口,請求天氣數據。
不同的提問可能需要查詢不同的接口數據,這個時候想把每個接口調用做成一個Python腳本插件,在程序運行過程中動態去請求不同接口,擴展性強。
def get_baidu_html(): import urllib2 import sys url = 'http://1212.ip138.com/ic.asp' html_content = urllib2.urlopen(url).read() return html_content
這個時候找到了【IronPython】,在C#程序里執行py腳本。
但是調試過程中出現了一個錯誤
“System.MissingMemberException”類型的未經處理的異常在 Microsoft.Dynamic.dll 中發生 其他信息: 'module' object has no attribute '_getframe'
搜了一下錯誤信息,已經有前人遇到過這個問題,但是並沒有給出最終解決方案 http://www.itnose.net/detail/6519438.html
[.NET&ironpython]C#調用python文件時出現'module' object has no attribute '_getframe'
郁悶了,估計是一個三方庫【import requests】和系統庫沖突
因為在正常使用Python自有類庫的時候沒有問題,只要引用了三方類庫,立馬報錯。
翻官方文檔發現
A new implementation-dependent function, sys._getframe([depth])(),
has been added to return a given frame object from the current call stack.
sys._getframe() returns the frame at the top of the call stack; if the optional integer argument depth is supplied,
the function returns the frame that is depth calls below the top of the stack. For example, sys._getframe(1) returns the caller’s frame object. This function is only present in CPython, not in Jython or the .NET implementation.
Use it for debugging, and resist the temptation to put it into production code.
大致意思是在NET里沒有實現這個 _getframe 方法,而requests用到了,自然報錯。
媽蛋的,此路不通,只能轉換思路,想其它方法實現需求了。requests 坑爹啊
第二天,今天翻牆google一搜,媽蛋的,直接第一個就是解決方案 http://stackoverflow.com/questions/6997832/ironpython-sys-getframe-not-found
var options = new Dictionary<string, object>(); options["Frames"] = true; options["FullFrames"] = true; ScriptEngine engine = Python.CreateEngine(options);
然並卵,按照上面的做了,又出現了新問題,無窮無盡。。。
“IronPython.Runtime.Exceptions.ImportException”類型的未經處理的異常在 Microsoft.Dynamic.dll 中發生
其他信息: No module named urllib3
pip install urllib3 解決,又報錯如下
“IronPython.Runtime.Exceptions.ImportException”類型的未經處理的異常在 Microsoft.Dynamic.dll 中發生
其他信息: No module named http_client
麻痹,沒完沒了的報錯。
不搞了,艹!