[英]How to fix web3 module AttributeError?
我正在尝试在 python 中使用 web3。
我正在尝试按照http://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth中的步骤进行操作
import web3
web3.eth.getBlock("1028201")
但是得到AttributeError: module 'web3.eth' has no attribute 'getBlock'
。
我在 python 3 和 python 2.7 中都试过,得到了相同的结果。
有什么建议吗?
谢谢
在调用web3.eth.getBlock
以设置eth
模块功能之前,请确保您正在实例化快速入门文档中提到的Web3
对象。
from web3 import Web3, TestRPCProvider
w3 = Web3(TestRPCProvider())
查看web3.eth的代码向我们展示了class Eth(Module):
包含def getBlock
。 如果您还查看Module
的定义,您会看到attach
函数实际用于根据您想要的行为重新定义web3.eth
。 attach
函数通常在web3/main.py
:
for module_name, module_class in modules.items():
module_class.attach(self, module_name)
请注意,在module_class
上方的循环之一中, Eth
和module_name
是"eth"
。
您可能缺少此逻辑,因此请确保在调用web3.eth.getBlock
之前实例化Web3
对象。
我正在为以太坊网络工作。 给定的代码对我有用。
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/"))
然后编写代码web3.eth.getBlock("1028201")
可以使用web3.eth.get_block
API 通过它们的编号或哈希来web3.eth.get_block
。 块哈希应采用十六进制表示。 块号
按数字获取块
web3.eth.get_block(12345)
见文档。 https://web3py.readthedocs.io/en/stable/examples.html#looking-up-blocks
如果有人像我一样来到这里寻找以下错误的解决方案
ImportError: cannot import name 'web3' from 'web3'
错误: from web3 import web3
正确: from web3 import Web3
请注意第二个“Web3”中的大写“W”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.