Blockchain and Python: Exploring the Python Blockchain Package

If you’ve ever wanted to interact with the Bitcoin blockchain directly, here’s your chance using Python! More specifically the python blockchain package interacts with the API.

Installing The Blockchain Package

Assuming you have access to the terminal with pip and/or Anaconda installed, you should be ready for the blockchain package installation.

Luckily, pip makes it easy to install the tool we will be using for this project. Open a terminal window and execute the following:

pip install blockchain

The blockexplorer Module

The first module we’re going to work with is blockexplorer. This allows us to lookup information on the Bitcoin blockchain. To do this, write the following as your first line of code:

from blockchain import blockexplorer as be

There are several object types we need to pay attention to. Block, Transaction, and Address will be the main 3. Coincidentally, we have get_block, get_tx, and get_address as information lookup methods for those three object types.

exampleBlock = be.get_block('000000000000000000e6257899a5a7994ca3c81315af180273de34c75f9d8933')
exampleTransaction = be.get_tx('726962b93d0f233d144dc9c8ada3a49251df804a135e43df5e8ac8e33182f985')
exampleAddress = be.get_address('1KuWLoZuoJgz3N6sLoAwGth9XGm8YuFTGt')

We can extract information from each of these objects.

  • Block – Hash, previous_block, time, fee, nonce, size, transactions, etc.
  • Transaction – block_height, time, relayed_by, hash, size, inputs, outputs, etc.
  • Address – hash, address, final_balance, total_received, transactions, etc.

What can we do with this information? We can track anything that appears on the blockchain!

The exchangerates Module

Besides the blockexplorer module, this package comes with a exchangerates import. Like last time, we want to import it like:

from blockchain import exchangerates as er

We can see how much bitcoin is going for in fiat currencies by printing a for loop of currency names and prices:

rates = er.get_ticker()
for i in rates:
	print(i, rates[i].p15min)

rates is a dictionary of currencies as keys and prices as values. My printed result was something like:

USD 2537.36
ISK 247468.72
HKD 19763.37
TWD 76392.95
CHF 2441.3
EUR 2249.06
DKK 16731.1
CLP 1700792.41
CAD 3421.76
INR 163444.04
CNY 17285.38
THB 86381.88
AUD 3409.05
SGD 3506.25
KRW 2836895.35
JPY 280160.07
PLN 9410.94
GBP 1967.96
SEK 21870.14
NZD 3552.23
BRL 8243.13
RUB 143698.31

These prices have many features including: last, buy, sell, and symbol.

Other Modules

The Python blockchain package has a few other modules to play around with:

  • statistics – Allows you to access the Bitcoin network’s statistics.
  • createwallet – Used for the creation of a new Bitcoin wallet.
  • pushtx – For broadcasting transactions.
  • And more!

I’ve highlighted the major functionality in the Python Blockchain package, but if you have any questions about other use cases, feel free to reach out!


Leave a Reply

Your email address will not be published. Required fields are marked *