# How to Generate an Ethereum Address (on console) ?

*Examining how to generate a random Ethereum wallet (address, public key, and private key) using Python on your local console through an example script.*

By [mberk.eth](https://paragraph.com/@mberketh) · 2024-06-28

coding, ethereum, python

---

**How to Generate an Ethereum Address Using Python**

Creating an Ethereum address involves a few cryptographic steps. In this article, we'll examine how to generate a random Ethereum wallet (address, public key, and private key) using Python on your local console.

This script uses the `eth_account` and `secrets` libraries to generate a random private key and derive an Ethereum account from this key. The account information is saved in an `ethereum_wallet.txt` file in the same directory.

For direct and detailed access to the code, you can visit the setup on GitHub.

First, you'll need `Python 3.x` and the `eth_account` library for this code.

### 1\. Generating a Private Key

A private key is a randomly generated 256-bit (32-byte) number, typically represented in hexadecimal (64 characters). This key allows you to sign transactions and access your funds.

*   Use a cryptographically secure random number generator to create a 256-bit number.
    

    import secrets from eth_account import Account
     
    #Generate a random private key 
    private_key = secrets.token_hex(32) 
    private_key_hex = "0x" + private_key 
    
    print(f'Private Key: {private_key_hex}')

The `secrets` library is used to generate cryptographically secure random numbers. This is crucial for tasks such as private key generation to minimize security vulnerabilities. By using this library, we ensure that the keys are unpredictable and secure.

    private_key_hex = "0x" + private_key

Here, the "0x" prefix indicates that the string represents a hexadecimal number.

### 2\. Generating the Public Key and Ethereum Address

The public key and Ethereum address are derived from the private key. The public key is used to verify transactions.

*   Create a public key from the private key and hash the public key to create the Ethereum address.
    

    from eth_account import Account 
    
    # Create an account from the private key
    account = Account.from_key(private_key_hex) 
    
    # Get the public key and Ethereum address
    public_key = account._key_obj.public_key 
    compressed_public_key = "0x" + public_key.to_compressed_bytes().hex() 
    
    print(f'Public Key:{compressed_public_key}') 
    print(f'Ethereum Address: {account.address}')

### 3\. Saving Information to a File (Optional)

You can save the generated private key, public key, and Ethereum address to a `.txt` file. Note: Storing the private key digitally can be risky.

Write the information to a `.txt` file in the specified format.

    # Write information to a .txt file (optional)
    with open("ethereum_wallet.txt", "w") as file: 
    	file.write(f"Address: {account.address}\n") 
    	file.write(f"Public: {compressed_public_key}\n") 
    	file.write(f"Private: {private_key_hex}\n") 
    
    print("Ethereum wallet information saved to ethereum_wallet.txt")

**Notes:**

*   Keep your private key secure and never share it. Losing the key or having it stolen can result in the loss of your funds.
    
*   The 256-bit length of the private key translates to 2^256 different combinations. Breaking such a large number is practically impossible. Even with the most powerful supercomputers, trying all possible combinations would take longer than the age of the universe.
    

In this example, we've covered generating an Ethereum address using the `eth_account` library. However, users can perform these basic cryptographic operations manually without the `eth_account` library, using libraries like `ecdsa` and `sha3` to create the public key and address.

**GitHub Repository**

_Cover is from ethereum.org_

_In my next article, we will discuss how mnemonic (seed) words are integrated into the process._

_This is a draft article. For errors, suggestions, and questions, please reach out at x.com/mberketh._

---

*Originally published on [mberk.eth](https://paragraph.com/@mberketh/ethereumwalletgenerator)*
