zoukankan      html  css  js  c++  java
  • 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe'

    最近在开发微信公众号,有一个自定义消息回复的需求

    比如用户:麻烦帮我查询一下北京的天气?
    
    系统回复:北京天气,晴,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

    麻痹,没完没了的报错。

    不搞了,艹!

  • 相关阅读:
    leecode4:寻找两个正序数组的中位数
    leecode3:无重复字符的最长子串
    leecode2:两数相加
    KMP字符串模式匹配
    01迷宫问题
    汉诺塔问题
    微服务-基于Grpc的进程通信-Grpc异常捕获RpcException(4-4)
    React-Antd Pro增删改查
    HTTP 请求
    创业路上-1
  • 原文地址:https://www.cnblogs.com/jhli/p/6208281.html
Copyright © 2011-2022 走看看