You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python blockchain客户端调用get_chart函数报未定义错误如何解决

解决方案

1 函数未定义报错修复

你出现报错的核心原因是导入模块后未通过模块命名空间调用函数,直接在全局域查找函数自然会提示未定义。你可以任选以下两种正确调用方式:

  • 方式1:通过导入的statistics模块调用
from blockchain import statistics

res = statistics.get_chart(chart_type="mempool-size", time_span="1year")
  • 方式2:直接从模块导入目标函数
    如果上述写法仍报错,大概率是你安装的SDK版本没有将get_chart暴露到模块顶层,可直接指定路径导入:
from blockchain.statistics import get_chart

res = get_chart(chart_type="mempool-size", time_span="1year")

如果还是调用失败,可按以下步骤排查:

  • 执行pip list | findstr blockchain(Windows)或pip list | grep blockchain(Linux/macOS)确认你安装的是官方blockchain.info SDK,而非其他同名第三方包
  • 确认你本地安装的SDK版本和文档对应,旧版本可能未收录get_chart方法,可执行pip install --upgrade blockchain升级到最新版本

2 替代获取数据方案

如果SDK调用持续有问题,可以直接请求blockchain.info的公开REST接口获取图表数据,无需依赖第三方SDK,稳定性更高。示例代码如下:

import requests

def get_chart_data(chart_type: str, time_span: str) -> dict:
    api_url = f"https://api.blockchain.info/charts/{chart_type}"
    query_params = {
        "timespan": time_span,
        "format": "json"
    }
    response = requests.get(api_url, params=query_params)
    # 接口异常时抛出错误
    response.raise_for_status()
    return response.json()

# 调用示例
mempool_data = get_chart_data("mempool-size", "1year")

接口返回的是标准JSON格式数据,包含图表名称、单位、时间序列数值等字段,可直接进行后续处理。

内容的提问来源于stack exchange,提问作者user14009897

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 14:15:03