solana-py no longer has solana.publickey
ImportError: 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.
Fix it fast
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 solders2Match what you see, then jump to its fix
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
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 solana3Verify
Both packages are listed and the client import works.
python -c "from solana.rpc.async_api import AsyncClient; print('ok')"Confirmed by michaelhly.com
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Python asyncio: Task was destroyed but it is pending
RuntimeWarning: coroutine '...' was never awaited
A task was still unfinished when Python threw it away.
4 causes, each with a fixBack end
Celery in a Django project does not connect to Redis or ignores the broker URL
kombu.exceptions.InconsistencyError: ... _kombu.binding.celery
Celery talks to Redis through a client library and reads its broker URL from your Django settings.
4 causes, each with a fixBack end
Django form POST returns 403 Forbidden: CSRF verification failed
CSRF verification failed. Request aborted.
Django rejects an unsafe request when the CSRF cookie or the form token is missing or wrong, or when the Origin or Referer check fails.
5 causes, each with a fixBack end
Django cannot import corsheaders (django-cors-headers)
ImportError: No module named corsheaders
The pip package is django-cors-headers, but the module Django imports is corsheaders.
3 causes, each with a fixBack end
Django raises NoReverseMatch when building a URL
NoReverseMatch
NoReverseMatch means Django looked for a named URL pattern that fits the name and arguments you gave and found none.
4 causes, each with a fix