trying to use web3.py but no methods seem to work

Question:

trying to use web3.py V5.31.1 with infura as a websocketprovider, but no methods seem to work.

The only method that works is w3.eth.chainId

my code:

from web3 import Web3
import web3

w3 = Web3(Web3.WebsocketProvider('wss://mainnet.infura.io/ws/v3/myapikey'))

print(w3.eth.chainId)

#w3.clientVersion --#doesnt work
#w3.is_connected() --#doesnt work

try:
print(web3.eth.get_block('latest'))
except:
print(web3.eth.getBlock('latest'))

error:

AttributeError: module 'web3.eth' has no attribute 'getBlock'
AttributeError: module 'web3.eth' has no attribute 'get_block'

any ideas why this could be happening?

Asked By: barryb

||

Answers:

You are confusing w3 instance and web3 module.

Please use:

print(w3.eth.get_block('latest'))
Answered By: Mikko Ohtamaa
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.