Python Web3: How to get an address from a private key?

Viewed 1173

I want to get the public address from a private key, which I know is feasible. How could I do so in using Python web3 library?

My attempt so far:

account = web3.eth.accounts.privateKeyToAccount(str(PrivateKey))

print(account.address)

Error: AttributeError: 'list' object has no attribute 'privateKeyToAccount'

2 Answers

You should create the account using from_key

from web3 import Web3
# Connect to Ganache, make sure ganache is running
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:8545"))

#Check Connection
t=w3.isConnected()
print(t)

# Get private key 
Private_key_Wallet_2 =  '0xc76...........8......24'

# Create a signer wallet
PA=w3.eth.account.from_key(Private_key_Wallet_2)

# Get public address from a signer wallet
Public_Address=PA.address
print(Public_Address)

web3.eth.accounts is a list and the syntax you're applying here implies you're working with something different. Doc
I suppose you looked in the web3 js documentations here which, in fact, does have said method and syntax combination.

Related