Learn WEB3PY with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Python Web3.py Simple Smart Contract Interaction
from web3 import Web3
# Connect to local Ethereum node
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
# Contract ABI and address (example)
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
# Read value from contract
value = contract.functions.getValue().call()
print('Contract value:', value)
# Send transaction to contract
tx_hash = contract.functions.setValue(42).transact({'from': w3.eth.accounts[0]})
w3.eth.wait_for_transaction_receipt(tx_hash)
A minimal example showing how to connect to Ethereum and interact with a deployed smart contract using Web3.py.
2
Web3.py Check Ether Balance
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
account = '0xYourAccountAddress'
balance = w3.eth.get_balance(account)
print('Balance:', w3.fromWei(balance, 'ether'), 'ETH')
Check the Ether balance of an account using Web3.py.
3
Web3.py Deploy Simple Contract
from web3 import Web3
from solcx import compile_source
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
source_code = 'contract Simple { uint public x; function set(uint val) public { x = val; } }'
compiled = compile_source(source_code)
contract_interface = compiled['<stdin>:Simple']
Simple = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Simple.constructor().transact({'from': w3.eth.accounts[0]})
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('Contract deployed at:', tx_receipt.contractAddress)
Deploy a simple Solidity contract using Web3.py.
4
Web3.py Listen to Contract Events
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
event_filter = contract.events.YourEvent.createFilter(fromBlock='latest')
for event in event_filter.get_new_entries():
print(event)
Subscribe to events emitted by a smart contract.
5
Web3.py Send Ether Transaction
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
from_account = w3.eth.accounts[0]
to_account = w3.eth.accounts[1]
tx = {
'from': from_account,
'to': to_account,
'value': w3.toWei(0.1, 'ether'),
'gas': 21000,
'gasPrice': w3.toWei('50', 'gwei')
}
tx_hash = w3.eth.send_transaction(tx)
w3.eth.wait_for_transaction_receipt(tx_hash)
Send Ether from one account to another using Web3.py.
6
Web3.py Interact with ERC20 Token
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xTokenContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
# Check token balance
balance = contract.functions.balanceOf(w3.eth.accounts[0]).call()
print('Token Balance:', balance)
# Transfer tokens
tx_hash = contract.functions.transfer(w3.eth.accounts[1], 100).transact({'from': w3.eth.accounts[0]})
w3.eth.wait_for_transaction_receipt(tx_hash)
Read balance and transfer ERC20 tokens.
7
Web3.py Estimate Gas
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
gas_estimate = contract.functions.setValue(42).estimateGas({'from': w3.eth.accounts[0]})
print('Estimated Gas:', gas_estimate)
Estimate gas required for a contract function call.
8
Web3.py Check Transaction Receipt
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
tx_hash = '0xYourTransactionHash'
receipt = w3.eth.get_transaction_receipt(tx_hash)
print(receipt)
Fetch and inspect a transaction receipt.
9
Web3.py Call Contract View Function
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
abi = '[...]'
address = '0xYourContractAddress'
contract = w3.eth.contract(address=address, abi=abi)
result = contract.functions.getValue().call()
print('Value:', result)
Call a read-only function from a deployed smart contract.
10
Web3.py Deploy Contract with Constructor Args
from web3 import Web3
from solcx import compile_source
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
source = 'contract Greeter { string public greeting; constructor(string memory _g) { greeting = _g; } }'
compiled = compile_source(source)
contract_interface = compiled['<stdin>:Greeter']
Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Greeter.constructor('Hello Web3').transact({'from': w3.eth.accounts[0]})
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('Deployed at:', tx_receipt.contractAddress)
Deploy a Solidity contract providing constructor parameters.