ModuleNotFoundError: 找不到'solana.publickey'模块求助
解决ModuleNotFoundError: 找不到'solana.publickey'模块的问题
问题说明
运行代码时触发ModuleNotFoundError,提示找不到'solana.publickey'模块,已尝试修改自身代码中的Pubkey引用,但问题仍未解决。使用的依赖版本:
- PySerum 0.5.0a0
- Solana 0.32.0
- Solders 0.20.0
运行代码
import solana import pyserum from pyserum.connection import conn from pyserum.market import Market from solana.rpc.api import Client from solders.pubkey import Pubkey # Connect to the Solana devnet http_client = Client("https://api.devnet.solana.com") connection = conn("https://api.devnet.solana.com") # Market address we will interact with market_address = "AMKpY1FVmZTRUDqCGyJvDBEYYqQwoqscu9J65MVXhkcT" # Fetch the market using the updated Pubkey reference market = Market.load(connection, Pubkey.from_string(market_address)) # Fetch the order book order_book = market.load_order_book() bids = order_book.bids() asks = order_book.asks() # Print the best bid and ask print(f"Best bid: {bids.get_best()}, Best ask: {asks.get_best()}")
错误信息
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[5], line 3 1 import solana 2 import pyserum ----> 3 from pyserum.connection import conn 4 from pyserum.market import Market 5 from solana.rpc.api import Client File ~/anaconda3/lib/python3.11/site-packages/pyserum/connection.py:6 3 import requests 5 from solana.rpc.api import Client as conn # pylint: disable=unused-import # noqa:F401 ----> 6 from solana.publickey import PublicKey 7 from .market.types import MarketInfo, TokenInfo 9 LIVE_MARKETS_URL = "https://raw.githubusercontent.com/project-serum/serum-ts/master/packages/serum/src/markets.json" ModuleNotFoundError: No module named 'solana.publickey'
解决方案
原因分析
Solana 0.32.0版本已将PublicKey类从solana.publickey模块迁移到了solders.pubkey模块,但你使用的PySerum 0.5.0a0内部代码仍在引用旧的模块路径,导致导入失败。
方案一:修改PySerum内部代码
- 找到PySerum的
connection.py文件,路径为~/anaconda3/lib/python3.11/site-packages/pyserum/connection.py - 打开文件,将第6行的
from solana.publickey import PublicKey替换为from solders.pubkey import Pubkey - 检查文件中其他是否有引用
PublicKey的地方,全部替换为Pubkey - 保存文件后重新运行代码
方案二:降级Solana版本
如果不想修改第三方库代码,可以将Solana降级到0.31.x版本(该版本仍保留solana.publickey模块),执行以下命令:
pip install solana==0.31.0
内容的提问来源于stack exchange,提问作者B S
相关产品推荐
相关产品推荐

