跳转到内容

Python SDK

Github Repo Stars

PyPI Version

Python Version

Python SDK 可让你连接、探索并与 Aptos 区块链交互。你可以用它请求数据、发送交易、设置测试环境等。

Aptos 在 PyPI 上提供官方 Python SDK,源代码位于 aptos-python-sdk GitHub 仓库。 该 SDK 需要 Python 3.12+

使用 pip 安装:

Terminal window
pip install aptos-sdk

或从源代码安装:

Terminal window
git clone https://github.com/aptos-labs/aptos-python-sdk
cd aptos-python-sdk
pip install .

以下片段取自 SDK 仓库中的 v2/examples/transfer_apt.py。 从 PyPI 安装时,请从 aptos_sdk.v2 导入相同 API,而不是 aptos_sdk_v2

transfer_apt.py
30 collapsed lines
"""Example: Transfer APT between accounts on devnet."""
import asyncio
from aptos_sdk_v2 import Account, Aptos, AptosConfig, Network
async def main():
config = AptosConfig(network=Network.DEVNET)
async with Aptos(config) as aptos:
# Create two accounts
alice = Account.generate()
bob = Account.generate()
print(f"Alice: {alice.address}")
print(f"Bob: {bob.address}")
# Fund Alice
print("\nFunding Alice...")
await aptos.faucet.fund_account(alice.address, 100_000_000)
# Fund Bob so account exists on-chain
print("Funding Bob...")
await aptos.faucet.fund_account(bob.address, 10_000_000)
# Check balances
alice_balance = await aptos.coin.balance(alice.address)
bob_balance = await aptos.coin.balance(bob.address)
print(f"\nAlice balance: {alice_balance}")
print(f"Bob balance: {bob_balance}")
# Transfer 1000 octas from Alice to Bob
print("\nTransferring 1000 octas from Alice to Bob...")
txn_hash = await aptos.coin.transfer(alice, bob.address, 1_000)
result = await aptos.transaction.wait_for_transaction(txn_hash)
print(f"Transaction: {txn_hash}")
print(f"Success: {result['success']}")
10 collapsed lines
# Check final balances
alice_balance = await aptos.coin.balance(alice.address)
bob_balance = await aptos.coin.balance(bob.address)
print(f"\nAlice balance: {alice_balance}")
print(f"Bob balance: {bob_balance}")
if __name__ == "__main__":
asyncio.run(main())