pip安装calplot模块后Python无法找到的问题求助
解决Python找不到已安装calplot模块的问题
问题描述
已通过pip安装calplot模块,但执行测试脚本时提示ModuleNotFoundError: No module named 'calplot',相关信息如下:
安装日志
$ pip install calplot Requirement already satisfied: calplot in /home/lamy/.local/lib/python3.10/site-packages (0.1.7.5) Requirement already satisfied: matplotlib in /home/lamy/.local/lib/python3.10/site-packages (from calplot) (3.7.1) Requirement already satisfied: numpy in /home/lamy/.local/lib/python3.10/site-packages (from calplot) (1.24.2) Requirement already satisfied: pandas>=1 in /home/lamy/.local/lib/python3.10/site-packages (from calplot) (2.0.0) Requirement already satisfied: pytz>=2020.1 in /home/lamy/.local/lib/python3.10/site-packages (from pandas>=1->calplot) (2023.3) Requirement already satisfied: python-dateutil>=2.8.2 in /home/lamy/.local/lib/python3.10/site-packages (from pandas>=1->calplot) (2.8.2) Requirement already satisfied: tzdata>=2022.1 in /home/lamy/.local/lib/python3.10/site-packages (from pandas>=1->calplot) (2023.3) Requirement already satisfied: pyparsing>=2.3.1 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (3.0.9) Requirement already satisfied: cycler>=0.10 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (0.11.0) Requirement already satisfied: kiwisolver>=1.0.1 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (1.4.4) Requirement already satisfied: fonttools>=4.22.0 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (4.39.3) Requirement already satisfied: pillow>=6.2.0 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (9.5.0) Requirement already satisfied: packaging>=20.0 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (23.0) Requirement already satisfied: contourpy>=1.0.1 in /home/lamy/.local/lib/python3.10/site-packages (from matplotlib->calplot) (1.0.7) Requirement already satisfied: six>=1.5 in /home/linuxbrew/.linuxbrew/lib/python3.10/site-packages (from python-dateutil>=2.8.2->pandas>=1->calplot) (1.16.0)
测试脚本CalPlot.py
import calplot import numpy as np; np.random.seed(sum(map(ord, 'calplot'))) import pandas as pd all_days = pd.date_range('1/1/2019', periods=730, freq='D') days = np.random.choice(all_days, 500) events = pd.Series(np.random.randn(len(days)), index=days) calplot.calplot(events)
执行错误
$ python CalPlot.py Traceback (most recent call last): File "/home/lamy/Documents/Git/Cs/Architecture/ArchitectureSrc/Hardware/Memory/Harddisk/Backup/Borg/Src/CalPlot.py", line 10, in <module> import calplot ModuleNotFoundError: No module named 'calplot'
环境信息
type pip pip is /home/linuxbrew/.linuxbrew/bin/pip pip is /usr/bin/pip pip is /bin/pip $ type python python is aliased to `python3' python is /usr/bin/python python is /bin/python $ type python3 python3 is /home/linuxbrew/.linuxbrew/bin/python3 python3 is /usr/bin/python3 python3 is /bin/python3 $ python --version Python 3.11.2 $ pip --version pip 23.0.1 from /home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/pip (python 3.11) $ python -m pip --version pip 23.0.1 from /home/linuxbrew/.linuxbrew/Cellar/python@3.11/3.11.2_1/lib/python3.11/site-packages/pip (python 3.11)
问题根源
从安装日志可以看到,calplot被安装到了Python 3.10的包目录/home/lamy/.local/lib/python3.10/site-packages,但当前执行脚本的Python版本是3.11.2,不同版本的Python包目录相互独立,所以3.11版本找不到3.10版本安装的模块。
解决方案
方法一:给当前Python版本重新安装calplot
当前默认pip对应Python 3.11,直接运行以下命令安装calplot到3.11的包目录:
pip install calplot
或者明确指定Python版本:
python3.11 -m pip install calplot
方法二:用安装了calplot的Python版本运行脚本
如果不想重新安装,找到Python 3.10的可执行文件路径(可以用which python3.10查看),然后用它来运行脚本:
python3.10 CalPlot.py
方法三:使用虚拟环境避免版本混乱
如果经常遇到多Python版本冲突问题,建议使用虚拟环境隔离依赖:
- 创建Python 3.11的虚拟环境:
python3.11 -m venv my_calplot_env
- 激活虚拟环境:
source my_calplot_env/bin/activate
- 在激活的环境中安装calplot:
pip install calplot
- 运行测试脚本:
python CalPlot.py
- 退出虚拟环境:
deactivate
内容的提问来源于stack exchange,提问作者Porcupine
相关产品推荐
相关产品推荐

