如何解决ModuleNotFoundError: No module named 'binance'及导入错误
解决python-binance包导入失败及
importlib.readers缺失问题 问题背景
- 在Jupyter Notebook执行
pip install binance后,导入from binance.client import Client提示ModuleNotFoundError: No module named 'binance' - 已完成排查:重命名本地
binance.py文件,确认未跨Python版本安装,但执行pip uninstall binance时提示该包未安装 - 改用
%conda install -c conda-forge python-binance安装后,导入时出现No module named 'importlib.readers'错误,conda list和pip list显示包已正常安装
报错回溯信息
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) Cell In[2], line 1 ----> 1 from binance.client import Client File ~\anaconda3\envs\py3\lib\site-packages\binance\__init__.py:9 1 """An unofficial Python wrapper for the Binance exchange API v3 2 3 .. moduleauthor:: Sam McHardy 4 5 """ 7 __version__ = '1.0.16' ----> 9 from binance.client import Client, AsyncClient # noqa 10 from binance.depthcache import DepthCacheManager, OptionsDepthCacheManager, ThreadedDepthCacheManager # noqa 11 from binance.streams import BinanceSocketManager, ThreadedWebsocketManager # noqa File ~\anaconda3\envs\py3\lib\site-packages\binance\client.py:7 5 import hashlib 6 import hmac ----> 7 import requests 8 import time 9 from operator import itemgetter File ~\anaconda3\envs\py3\lib\site-packages\requests\__init__.py:147 144 import logging 145 from logging import NullHandler --> 147 from . import packages, utils 148 from .__version__ import ( 149 __author__, 150 __author_email__, (...) 158 __version__, 159 ) 160 from .api import delete, get, head, options, patch, post, put, request File ~\anaconda3\envs\py3\lib\site-packages\requests\utils.py:58 54 from .structures import CaseInsensitiveDict 56 NETRC_FILES = (".netrc", "_netrc") ---> 58 DEFAULT_CA_BUNDLE_PATH = certs.where() 60 DEFAULT_PORTS = {"http": 80, "https": 443} 62 # Ensure that ', ' is used to preserve previous delimiter behavior. File ~\anaconda3\envs\py3\lib\site-packages\certifi\core.py:71, in where() 58 global _CACERT_PATH 59 if _CACERT_PATH is None: 60 # This is slightly janky, the importlib.resources API wants you 61 # to manage the cleanup of this file, so it doesn't actually (...) 69 # it will do the cleanup whenever it gets garbage collected, so 70 # we will also store that at the global level as well. ---> 71 _CACERT_CTX = get_path("certifi", "cacert.pem") 72 _CACERT_PATH = str(_CACERT_CTX.__enter__()) 74 return _CACERT_PATH File ~\anaconda3\envs\py3\lib\importlib\resources.py:119, in path(package, resource) 112 else: 113 return BytesIO(data) 116 def open_text(package: Package, 117 resource: Resource, 118 encoding: str = 'utf-8', --> 119 errors: str = 'strict') -> TextIO: 120 """Return a file-like object opened for text reading of the resource.""" 121 resource = _normalize_path(resource) File ~\anaconda3\envs\py3\lib\importlib\_common.py:52, in get_resource_reader(package) ModuleNotFoundError: No module named 'importlib.readers'
解决方案
- 修复
importlib.readers缺失问题:该模块是Python 3.10及以上版本的内置模块,若环境Python版本低于3.10,可二选一操作:- 升级Python到3.10及以上版本
- 降级certifi包到兼容旧Python版本的版本:
pip install certifi==2022.12.07
- 彻底重装python-binance:
- 卸载所有相关包:
pip uninstall -y python-binance conda uninstall -y python-binance - 清理安装缓存:
pip cache purge conda clean --all - 用pip重新安装(优先选择pip而非conda,减少依赖冲突):
pip install python-binance
- 卸载所有相关包:
- 验证Jupyter内核环境:在Notebook中执行以下代码,确认使用的Python路径与安装包的环境一致:
若路径不符,需切换Jupyter内核到对应conda环境。import sys print(sys.executable)
内容的提问来源于stack exchange,提问作者Eli Bain
相关产品推荐
相关产品推荐

