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 aTransactionNotFound
errorgetTransactionReceipt()
will throw aTransactionNotFound
errorgetTransactionByBlock()
will throw aTransactionNotFound
errorgetTransactionCount()
will throw aBlockNotFound
errorgetBlock()
will throw aBlockNotFound
errorgetUncleCount()
will throw aBlockNotFound
errorgetUncleByBlock()
will throw aBlockNotFound
error
Removed Methods
contract.buildTransaction
was removed forcontract.functions.buildTransaction.<method name>
contract.deploy
was removed forcontract.constructor.transact
contract.estimateGas
was removed forcontract.functions.<method name>.estimateGas
contract.call
was removed forcontract.<functions/events>.<method name>.call
contract.transact
was removed forcontract.<functions/events>.<method name>.transact
contract.eventFilter
was removed forcontract.events.<event name>.createFilter
middleware_stack
was renamed tomiddleware_onion()
web3.miner.hashrate
was a duplicate ofhashrate()
and was removed.web3.version.network
was a duplicate ofversion()
and was removed.web3.providers.tester.EthereumTesterProvider
andweb3.providers.tester.TestRPCProvider
have been removed forEthereumTesterProvider()
web3.eth.enableUnauditedFeatures
was removedweb3.txpool
was moved totxpool()
web3.version.node
was removed forweb3.clientVersion
web3.version.ethereum
was removed forprotocolVersion()
Relocated personal RPC endpoints to reflect Parity and Geth implementations:
web3.personal.listAccounts
was removed forlistAccounts()
orlistAccounts()
web3.personal.importRawKey
was removed forimportRawKey()
orimportRawKey()
web3.personal.newAccount
was removed fornewAccount()
ornewAccount()
web3.personal.lockAccount
was removed forlockAccount()
web3.personal.unlockAccount
was removed forunlockAccount()
orunlockAccount()
web3.personal.sendTransaction
was removed forsendTransaction()
orsendTransaction()
Relocated
web3.admin
module toweb3.geth
namespaceRelocated
web3.miner
module toweb3.geth
namespace
Deprecated Methods
Expect the following methods to be removed in v6:
web3.sha3
was deprecated forkeccak()
web3.soliditySha3
was 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.