Marketplace has launched, further enabling blockchain developers! Learn more

How to connect to the Ethereum network using Python with Web3.py

September 23, 2022

Overview


You can build Ethereum applications in different programming languages. In this article, we will connect to the Ethereum network using Python.


Prerequisite

  • Ethereum Node (We will use QuickNode’s free version)
  • Python installed in your system (version >=3.5.3) and Pip3
  • Install web3.py (A Python wrapper for Ethereum node APIs)
  • A text editor

Note:
We need Python version >=3.5.3 and install web3.py using pip3 install web3.


Python and other library versions are the cause of the common installation problems. Therefore, if you face any problem, try setting up a virtual environment and troubleshoot web3.py installation.


Connecting to Ethereum Blockchain

A network of Ethereum nodes is called Ethereum blockchain. To interact with Etherum blockchain, these nodes expose APIs which are available using web3.py library.

In addition, we will need a running Ethereum node, therefore, we will use QuickNode’s free version. Just sign up here and get the node URL.

Screenshot of Quicknode Ethereum Endpoint


We will use web3.py to get the latest Ethereum block number. For this, we will use the code snippet given below. 

connecting to ethereum blockchain

Copy
from web3 import Web3, HTTPProvider
w3 = Web3(HTTPProvider('YOUR_ETHEREUM_NODE_URL'))
print ("Latest Ethereum block number" , w3.eth.blockNumber)

In this snippet, we are importing web3.py library and adding our Ethereum node URL and getting the latest Ethereum block number using w3.eth.blockNumber API.

Now, we will save this code snippet in index.py file and then run it using python index.py command. After running this command we will see the latest Ethereum block number as shown below.


That’s it you have connected through the Ethereum network using Python. In addition, you can learn more web3.py APIs to build complex applications on Ethereum.

Related articles 36

How to Access Bitcoin Mempool
Published: Aug 16, 2021
Updated: Sep 21, 2022

Bitcoin is the father of blockchain technology. With Bitcoin started a new era of blockchain and decentralization. Bitcoin enabled everyone to make the peer-to-peer transactions they enjoy...

Continue reading
How to access Ethereum Mempool
Published: Oct 15, 2020
Updated: Sep 15, 2022

On Ethereum, when a transaction is sent, before being added to a block, it resides in what is called a Mempool. To receive information about this transaction, the Mempool must be queried. This...

Continue reading
The Web3 Developer Stack
Published: Jul 3, 2021
Updated: Sep 15, 2022

A developer stack is a bag of technologies a developer possesses. For example, MEAN (MongoDB, Express.js, AngularJS/Angular, and Node.js) and MERN (MongoDB, Express.js, React, and Node.js) are...

Continue reading
Introduction to Scaffold-ETH 🏗
Published: Oct 7, 2021
Updated: Sep 9, 2022

Developing applications involves juggling several moving pieces like front-ends, back-ends, and databases. But developing a decentralized application on a blockchain adds a few more elements...

Continue reading
How to create your own DAO with Aragon
Published: Aug 8, 2021
Updated: Sep 9, 2022

Blockchain provides us with the power of decentralization. Decentralization means the transfer of power to users/members rather than having a single centralized authority governing everything;...

Continue reading
How to connect to Ethereum using PHP
Published: Jun 11, 2021
Updated: Sep 9, 2022

PHP is a very popular choice among developers and has a vast community due to its long presence in web development. In this guide, we’ll cover how to connect to Ethereum with PHP using the

Continue reading
How to use Subspace with QuickNode
Published: Sep 8, 2020
Updated: Sep 9, 2022

In this guide, we'll understand a bit about reactive development and how to use Subspace with QuickNode.JavaScript is the programming language behind most of the internet apps and...

Continue reading