[英]How to fix web3 module AttributeError?
I am trying to using web3 in python.我正在尝试在 python 中使用 web3。
I am trying to follow the steps in http://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth我正在尝试按照http://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth中的步骤进行操作
import web3
web3.eth.getBlock("1028201")
But got AttributeError: module 'web3.eth' has no attribute 'getBlock'
.但是得到
AttributeError: module 'web3.eth' has no attribute 'getBlock'
。
I tried in both python 3 and python 2.7, got the same result.我在 python 3 和 python 2.7 中都试过,得到了相同的结果。
Any suggestion?有什么建议吗?
Thanks谢谢
Ensure you are instantiating a Web3
object as mentioned in the quickstart docs before calling into web3.eth.getBlock
to setup the eth
module functions.在调用
web3.eth.getBlock
以设置eth
模块功能之前,请确保您正在实例化快速入门文档中提到的Web3
对象。
from web3 import Web3, TestRPCProvider
w3 = Web3(TestRPCProvider())
A look at the code for web3.eth shows us that class Eth(Module):
contains def getBlock
.查看web3.eth的代码向我们展示了
class Eth(Module):
包含def getBlock
。 If you also look at the definition of Module
, you see see that the attach
function is used to actually redefine web3.eth
with the behavior you want.如果您还查看
Module
的定义,您会看到attach
函数实际用于根据您想要的行为重新定义web3.eth
。 The attach
function is normally called in the web3/main.py
: attach
函数通常在web3/main.py
:
for module_name, module_class in modules.items():
module_class.attach(self, module_name)
Note in one of the loops above the module_class
is Eth
and module_name
is "eth"
.请注意,在
module_class
上方的循环之一中, Eth
和module_name
是"eth"
。
You are likely missing this logic, so ensure you are instantiating a Web3
object before calling into web3.eth.getBlock
.您可能缺少此逻辑,因此请确保在调用
web3.eth.getBlock
之前实例化Web3
对象。
I am working for Ethereum network.我正在为以太坊网络工作。 Given code works for me.
给定的代码对我有用。
from web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://ropsten.infura.io/"))
And then write code web3.eth.getBlock("1028201")
然后编写代码
web3.eth.getBlock("1028201")
Blocks can be looked up by either their number or hash using the web3.eth.get_block
API.可以使用
web3.eth.get_block
API 通过它们的编号或哈希来web3.eth.get_block
。 Block hashes should be in their hexadecimal representation.块哈希应采用十六进制表示。 Block numbers
块号
get a block by number按数字获取块
web3.eth.get_block(12345)
See doc.见文档。 https://web3py.readthedocs.io/en/stable/examples.html#looking-up-blocks
https://web3py.readthedocs.io/en/stable/examples.html#looking-up-blocks
If anyone landed here in search of solution of below error like me如果有人像我一样来到这里寻找以下错误的解决方案
ImportError: cannot import name 'web3' from 'web3'
wrong: from web3 import web3
correct: from web3 import Web3
Please note capital 'W' in second 'Web3'错误:
from web3 import web3
正确: from web3 import Web3
请注意第二个“Web3”中的大写“W”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.