Registry / serialization / substrate-interface

substrate-interface

JSON →
library1.8.1pypypi✓ verified 85d ago

The `substrate-interface` library provides a Pythonic way to interact with Substrate-based blockchain nodes, including Polkadot and Kusama. It supports both traditional RPC connections and lightweight client functionality via Smoldot integration introduced in v1.8.0. The library enables querying chain state, submitting extrinsics, and managing subscriptions. It is actively maintained with frequent updates, currently at version 1.8.1.

pip install substrate-interface
INSTALL
IMPORT
SIG · SUBSTRATE-INTERFAC
S
substrate-interface
serializationpythonv1.8.1
Install
7.0s avg
Import
1404ms
Disk
89MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.980 runs
installs and imports cleanly · install 0.0s · import 1.458s · 82.1MB
glibc
py 3.103.980 runs
installs and imports cleanly · install 7.0s · import 1.350s · 96MB
89MB installed
● package 89MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SubstrateInterface
from substrateinterface import SubstrateInterface

This quickstart connects to a public Polkadot RPC endpoint, queries the chain and node information, and then retrieves the balance for a well-known test address. It demonstrates basic connection and state querying. Ensure you have network access to the specified node URL.

import os from substrateinterface import SubstrateInterface # Connect to a public Polkadot node by default # Override with environment variable SUBSTRATE_NODE_URL if needed node_url = os.environ.get('SUBSTRATE_NODE_URL', 'wss://rpc.polkadot.io') try: substrate = SubstrateInterface( url=node_url ) print(f"Connected to chain: {substrate.chain}") print(f"Node name: {substrate.node_name}") print(f"Node version: {substrate.node_version}") print(f"Current block number: {substrate.chain_head}") # Example: Query account balance for a well-known test address (Alice) # Override with environment variable POLKADOT_ALICE_ADDRESS if needed alice_address = os.environ.get('POLKADOT_ALICE_ADDRESS', '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') # The 'System' module and 'Account' storage map are standard across Substrate chains. # The exact structure of the account info might vary slightly by chain/runtime version. result = substrate.query( module='System', function='Account', params=[alice_address] ) # Accessing the free balance, typically found under 'data' and 'free'. # Assuming 10 decimal places for Polkadot for display purposes. # The '.value' attribute extracts the raw integer. free_balance = result['data']['free'].value print(f"Balance for {alice_address}: {free_balance / 10**10} DOT") except ConnectionRefusedError: print(f"Error: Connection refused. Is the node running or accessible at {node_url}?") except Exception as e: print(f"An unexpected error occurred: {e}") finally: if 'substrate' in locals() and substrate.is_connected: substrate.close()
Debug
Known issues
gotchaThe `eth-utils` dependency frequently receives updates and has tight version pinning, which can lead to conflicts if other libraries in your environment require a different, incompatible version range.
fix
Upgrade `substrate-interface` to the latest version to ensure compatibility. If conflicts persist, consider using a dedicated virtual environment for your project or explicitly pinning `eth-utils` to a version compatible with all your installed libraries (e.g., `pip install "eth-utils<6"` if other libraries require earlier versions, but this might break `substrate-interface`).
affects: All versions, particularly around major `eth-utils` bumps (e.g., v1.7.9, v1.7.11, v1.8.0)
breakingSubstrate runtimes (and thus their metadata) are constantly evolving. Older versions of `substrate-interface` may not correctly parse or interact with newer runtime metadata (e.g., MetadataV15, Ink! V5 contracts) and vice-versa.
fix
Always keep `substrate-interface` updated to the latest stable version to ensure compatibility with the most recent Substrate runtime metadata. Check release notes for specific metadata version support.
affects: Pre-v1.7.10 (Ink! V5), Pre-v1.8.0a (MetadataV15), and potentially others with significant runtime changes.
deprecatedCertain older RPC calls or methods for interacting with node metadata may be deprecated or removed in newer versions of `substrate-interface` or on newer Substrate runtimes, leading to `ModuleNotFound` or `AttributeError`.
fix
Refer to the latest `substrate-interface` documentation and examples. Upgrade the library to benefit from updated and supported methods. If migrating from older code, review the changelog for removed or altered functionality.
affects: Versions prior to v1.7.6, where deprecated calls were explicitly removed; continuous evolution.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'smoldot_module_py'
The `smoldot` light client dependency is optional and not installed by default with the base `pip install substrate-interface` command.
fix
Install `substrate-interface` with the `lightclient` extra: `pip install "substrate-interface[lightclient]"`.
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
The WebSocket connection to the Substrate node was unexpectedly terminated or could not be established. This often indicates the node is offline, inaccessible, or the URL is incorrect.
fix
Verify the Substrate node is running and accessible at the specified URL (e.g., `ws://127.0.0.1:9944` or `wss://rpc.polkadot.io`). Check firewall rules or ensure the node is properly configured to accept connections.
ConnectionRefusedError: [Errno 111] Connection refused
The attempt to connect to the Substrate node at the specified address and port was actively refused. This typically means there is no service listening on that port, or a firewall is blocking the connection.
fix
Ensure the Substrate node is running and listening on the expected address and port. Verify the `url` parameter in `SubstrateInterface()` is correct. Check local and network firewall configurations.
KeyError: 'some_expected_key_here'
When querying chain state or decoding extrinsic results/events, an expected dictionary key (representing a module, function, or data field) was not found. This often points to a metadata mismatch or an incorrect understanding of the runtime's data structure.
fix
Ensure `substrate-interface` is updated to the latest version to match the connected node's runtime metadata. Review the specific Substrate chain's documentation or use `substrate.get_metadata_dict()` to inspect the exact structure of modules, calls, and storage maps.
Upgrade
Version history
1.8.1latest on PyPI · released Jan 20, 2026
Audit
Dependencies
scale-codecrequiredCore SCALE encoding/decoding for Substrate data types.
websocket-clientrequiredRequired for establishing WebSocket RPC connections to Substrate nodes.
eth-utilsrequiredUtility functions, frequently updated and can cause conflicts with other libraries.
logururequiredLogging library.
toolzrequiredFunctional programming tools.
xxh64requiredXXH64 hash algorithm implementation.
ecdsarequiredElliptic Curve Digital Signature Algorithm implementation.
smoldotoptionalProvides a Rust-based WebAssembly light client for embedded node connectivity. Requires `[lightclient]` extra.
py-sr25519optionalSR25519 cryptography library for signing operations. Requires `[sr25519]` extra.
py-ed25519optionalEd25519 cryptography library for signing operations. Requires `[ed25519]` extra.
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
2
Resources
substrate-interface — pip install substrate-interface · libregistry