Migrating your code from v4 to v5
Web3.py follows Semantic Versioning, which means that version 5 introduced backwards-incompatible changes. If your project depends on Web3.py v4, then you’ll probably need to make some changes.
Here are the most common required updates:
Python 3.5 no longer supported
You will need to upgrade to either Python 3.6 or 3.7
eth-abi v1 no longer supported
You will need to upgrade the eth-abi dependency to v2
Changes to base API
JSON-RPC Updates
In v4, JSON-RPC calls that looked up transactions or blocks and
didn’t find them, returned None. Now if a transaction or
block is not found, a BlockNotFound or a TransactionNotFound
error will be thrown as appropriate. This applies to the
following web3 methods:
getTransaction()will throw aTransactionNotFounderrorgetTransactionReceipt()will throw aTransactionNotFounderrorgetTransactionByBlock()will throw aTransactionNotFounderrorgetTransactionCount()will throw aBlockNotFounderrorgetBlock()will throw aBlockNotFounderrorgetUncleCount()will throw aBlockNotFounderrorgetUncleByBlock()will throw aBlockNotFounderror
Removed Methods
contract.buildTransactionwas removed forcontract.functions.buildTransaction.<method name>contract.deploywas removed forcontract.constructor.transactcontract.estimateGaswas removed forcontract.functions.<method name>.estimateGascontract.callwas removed forcontract.<functions/events>.<method name>.callcontract.transactwas removed forcontract.<functions/events>.<method name>.transactcontract.eventFilterwas removed forcontract.events.<event name>.createFiltermiddleware_stackwas renamed tomiddleware_onion()web3.miner.hashratewas a duplicate ofhashrate()and was removed.web3.version.networkwas a duplicate ofversion()and was removed.web3.providers.tester.EthereumTesterProviderandweb3.providers.tester.TestRPCProviderhave been removed forEthereumTesterProvider()web3.eth.enableUnauditedFeatureswas removedweb3.txpoolwas moved totxpool()web3.version.nodewas removed forweb3.clientVersionweb3.version.ethereumwas removed forprotocolVersion()Relocated personal RPC endpoints to reflect Parity and Geth implementations:
web3.personal.listAccountswas removed forlistAccounts()orlistAccounts()web3.personal.importRawKeywas removed forimportRawKey()orimportRawKey()web3.personal.newAccountwas removed fornewAccount()ornewAccount()web3.personal.lockAccountwas removed forlockAccount()web3.personal.unlockAccountwas removed forunlockAccount()orunlockAccount()web3.personal.sendTransactionwas removed forsendTransaction()orsendTransaction()
Relocated
web3.adminmodule toweb3.gethnamespaceRelocated
web3.minermodule toweb3.gethnamespace
Deprecated Methods
Expect the following methods to be removed in v6:
web3.sha3was deprecated forkeccak()web3.soliditySha3was deprecated forsolidityKeccak()chainId()was deprecated forchainId(). Follow issue #1293 for detailsweb3.eth.getCompilers()was deprecated and will not be replacedgetTransactionFromBlock()was deprecated forgetTransactionByBlock()
Deprecated ConciseContract and ImplicitContract
The ConciseContract and ImplicitContract have been deprecated and will be removed in v6.
ImplicitContract instances will need to use the verbose syntax. For example:
contract.functions.<function name>.transact({})
ConciseContract has been replaced with the ContractCaller API. Instead of using the ConciseContract factory, you can now use:
contract.caller.<function_name>
or the classic contract syntax:
contract.functions.<function name>.call().
Some more concrete examples can be found in the ContractCaller docs
Manager Provider
In v5, only a single provider will be allowed. While allowing multiple providers is a feature we’d like to support in the future, the way that multiple providers was handled in v4 wasn’t ideal. The only thing they could do was fall back. There was no mechanism for any round robin, nor was there any control around which provider was chosen. Eventually, the idea is to expand the Manager API to support injecting custom logic into the provider selection process.
For now, manager.providers has changed to manager.provider.
Similarly, instances of web3.providers have been changed to
web3.provider.
Testnet Changes
Web3.py will no longer automatically look up a testnet connection in IPCProvider.
ENS
Web3.py has stopped inferring the .eth TLD on domain names.
If a domain name is used instead of an address, you’ll need
to specify the TLD. An InvalidTLD error will be thrown if
the TLD is missing.
Required Infura API Key
In order to interact with Infura after March 27, 2019, you’ll need to set an
environment variable called WEB3_INFURA_PROJECT_ID. You can get a
project id by visiting https://infura.io/register.