您的位置:首页 > 移动开发 > Objective-C

【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

2016-12-21 16:55 766 查看
最近在开发微信公众号,有一个自定义消息回复的需求

比如用户:麻烦帮我查询一下北京的天气?

系统回复:北京天气,晴,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 坑爹啊

第二天,今天FQgoogle一搜,妈蛋的,直接第一个就是解决方案 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


麻痹,没完没了的报错。

不搞了,艹!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐