githubEdit

Interact with Ethereum using Python

Preparation

To use “py-solc”, the Ethereum and Solidity are required in our system. So if you don’t have them yet, install them.

sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc

Install Python Packages

pip3 install py-solc
pip3 install web3

Compile Contract

import solc

with open('MyContract.sol', 'r') as f:
    contract_source_code = f.read()

compiled_sol = solc.compile_source(contract_source_code)

contract_bytecode = compiled_sol['<stdin>:MyContract']['bin']
contract_abi = compiled_sol['<stdin>:MyContract']['abi']

Interact with Ethereum Chain

Create the Python script using web3 to interact with blockchain.

Last updated