Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

solana-py no longer has solana.publickey

ErrorImportError: cannot import name 'PublicKey' from 'solana.publickey'

Short answer

solana-py now takes its core types from the solders package, so old tutorial code that imports PublicKey from solana.publickey no longer works. Install solders and import Pubkey from solders.pubkey. Update the old code, or pin the library to an old release.

  • Applies to solana-py (current), solders (current), Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the installed versions of both packages, since the import path depends on which solana-py you have.

    pip show solana solders
  2. 2Match what you see, then jump to its fix

    1. 1The tutorial or repo imports PublicKey from solana.publickeyCode written for an old solana-py, running on a new oneFix
    2. 2ModuleNotFoundError: No module named 'solders'solders is not installedFix

What does the error look like?

  • ImportError: cannot import name 'PublicKey' from 'solana.publickey'
  • ModuleNotFoundError: No module named 'solana.publickey'

Cause 1 of 2

Code written for an old solana-py, running on a new one

1Check

The README says the library uses core types from solders, which solana-py used to provide itself, and that anything missing after an upgrade is probably in solders now.

grep -rn "solana.publickey\|PublicKey" --include=*.py .

2Fix

Use Pubkey from solders. Pubkey.from_string reads a base58 string.

from solders.pubkey import Pubkey

address = Pubkey.from_string("BPFLoader1111111111111111111111111111111111")

3Verify

The import runs and prints the address.

python -c "from solders.pubkey import Pubkey; print(Pubkey.from_string('BPFLoader1111111111111111111111111111111111'))"

Confirmed by kevinheavey.github.io

Did this fix it?

Cause 2 of 2

solders is not installed

1Check

The install instructions list two packages: solders first, then solana.

pip list | grep -i -E "solana|solders"

2Fix

Install both, in a virtual environment.

pip install solders
pip install solana

3Verify

Both packages are listed and the client import works.

python -c "from solana.rpc.async_api import AsyncClient; print('ok')"

Confirmed by michaelhly.com

Did this fix it?

How do I stop it coming back?

  • Pin solana and solders versions in requirements.txt so an upgrade is a decision, not a surprise.
  • When a tutorial is older than the library, read the library's changelog before copying code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.