如何修复web3模块AttributeError?

时间:2018-01-10 06:09:47

标签: python ethereum web3

我正在尝试在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中都尝试过,得到了相同的结果。

有什么建议吗?

由于

3 个答案:

答案 0 :(得分:1)

确保在调用Web3以设置web3.eth.getBlock模块函数之前,正在实例化quickstart docs中提到的eth对象。

from web3 import Web3, TestRPCProvider
w3 = Web3(TestRPCProvider())

查看code for web3.eth向我们显示class Eth(Module):包含def getBlock。如果您还查看definition of Module,则会看到attach函数用于实际重新定义web3.eth所需的行为。 attach函数通常在web3/main.py

中调用
for module_name, module_class in modules.items():
        module_class.attach(self, module_name)

请注意,module_class上方的其中一个循环为Ethmodule_name"eth"

您可能缺少此逻辑,因此请确保在调用Web3之前实例化web3.eth.getBlock对象。

答案 1 :(得分:1)

我正在为以太坊网络工作。给定的代码对我有用。

from web3 import Web3                               
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/"))

然后编写代码web3.eth.getBlock("1028201")

答案 2 :(得分:0)

块可以使用 web3.eth.get_block API 通过其编号或哈希值进行查找。块哈希应采用十六进制表示。块号

按数字获取区块

web3.eth.get_block(12345)

见文档。 https://web3py.readthedocs.io/en/stable/examples.html#looking-up-blocks