Tronpy

raw JSON →
0.6.2 verified Mon Apr 27 auth: no python

Tronpy is a Python client library for interacting with the TRON blockchain. It supports TRON's full node API, TRON Grid API, staking, smart contracts, and transaction building. Current version is 0.6.2, compatible with Python >=3.9. Release cadence is roughly every 3-6 months with feature updates and Python version support bumps.

pip install tronpy
error AttributeError: module 'tronpy' has no attribute 'Tron'
cause Incorrect import path; Tron is not directly under tronpy due to import errors in older versions or when using a different structure.
fix
Use from tronpy import Tron (correct). Ensure you have installed the latest version.
error ImportError: cannot import name 'HTTPProvider' from 'tronpy.providers'
cause The provider module was renamed or restructured in earlier versions (before 0.3.0).
fix
Upgrade tronpy to version 0.3.0 or later. Then from tronpy.providers import HTTPProvider.
breaking In v0.4.0, stake V1 APIs were removed in favor of stake V2. Old stake methods (freezeBalance, unfreezeBalance) no longer work. Use stake_v2 methods instead.
fix Migrate to stake V2 API: use `client.stake_v2` methods like `freeze_balance_v2` and `unfreeze_balance_v2`.
deprecated Support for Python 3.8 was dropped in v0.6.0. The package now requires Python >=3.9.
fix Upgrade Python to 3.9 or higher.
gotcha When using offline transaction creation (v0.6.0+), you must provide `ref_block_id` and `offline=True` to `build()`. Forgetting `ref_block_id` will cause transaction rejection on the network.
fix Ensure you include `ref_block_id` from a recent block when building offline transactions.
gotcha The `get_energy` method was added in v0.6.0. In earlier versions, energy estimation required a different approach (e.g., using `trigger_constant_contract`).
fix Upgrade to 0.6.0+ for `get_energy` method.
pip install tronpy[offline]

Initialize a Tron client with HTTPProvider (optionally using TRON Grid API key) and fetch latest block number.

from tronpy import Tron
from tronpy.providers import HTTPProvider

client = Tron(provider=HTTPProvider(api_key=os.environ.get('TRONGRID_API_KEY', '')))
# Get latest block number
block = client.get_latest_block_number()
print(f"Latest block: {block}")