[英]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.