VS Code无法识别已安装的matplotlib库,求助解决方法
问题详情
此前项目已安装matplotlib且使用正常,今日添加直方图时VS Code突然无法识别该库,报错:
line 1, in <module> import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'matplotlib'
已尝试重启VS Code、升级pip、重装matplotlib,问题仍未解决。
相关代码片段:
import matplotlib.pyplot as plt import matplotlib import numpy as np from matplotlib.animation import FuncAnimation class Particelle(): def __init__(self, id=0, r=np.zeros(2), v=np.zeros(2), R=1E-2,m=1): #valori di default self.id, self.r, self.v, self.R, self.m= id,r,v,R,m #valori individuali class Sis(): x=2 y=2 def __init__(self, t=1E-1, N=1): self.t, self.N= t,N self.particelle=[Particelle(i) for i in range(self.N)] self.entropia_values = [] def collision_detection(self): ignore_list = [] for particella1 in self.particelle: if particella1 in ignore_list: continue x, y = particella1.r if ((x > self.x/2 - particella1.R) or (x < -self.x/2+particella1.R)): particella1.v[0] *= -1 if ((y > self.y/2 - particella1.R) or (y < -self.y/2+particella1.R)): particella1.v[1] *= -1 for particella2 in self.particelle: if id(particella1) == id(particella2): continue m1, m2, r1, r2, v1, v2 = particella1.m, particella2.m, particella1.r, particella2.r, particella1.v, particella2.v if np.dot(r1-r2, r1-r2) <= (particella1.R + particella2.R)**2: v1_new = v1 - 2*m1 / \ (m1+m2) * np.dot(v1-v2, r1-r2) / \ np.dot(r1-r2, r1-r2)*(r1-r2) v2_new = v2 - 2*m1 / \ (m1+m2) * np.dot(v2-v1, r2-r1) / \ np.dot(r2-r1, r2-r1)*(r2-r1) particella1.v = v1_new particella2.v = v2_new ignore_list.append(particella2) def increment(self): ignore_list = self.collision_detection() for particelle in self.particelle: particelle.r += self.t * particelle.v entropia_val = self.entropia() self.entropia_values.append(entropia_val) def posizione_particelle(self): return[particelle.r for particelle in self.particelle ] def velocità_particelle(self): return[np.sqrt(np.dot(particelle.v, particelle.v )) for particelle in self.particelle ] def entropia(self): entropia = 0 for particella in self.particelle: entropia -= particella.m * np.log(np.linalg.norm(particella.v)) return entropia N=100 sis=Sis(N=N) for particelle in sis.particelle: particelle.r = np.random.uniform( [-sis.x/2, -sis.y/2], [sis.x/2, sis.y/2], size=2) particelle.v = 1*np.array([np.cos(np.pi/4), np.sin(np.pi/4)]) fig, (ax1, ax2 ) = plt.subplots(1, 2, figsize=(10, 4)) scatter = ax1.scatter([], []) entropia, = ax2.plot([], []) vm=np.linspace(0,2,10)#velocità media def update(frame): sis.increment() scatter.set_offsets(np.array(sis.posizione_particelle())) entropia.set_data(range(frame + 1), sis.entropia_values) return scatter, entropia, def init(): ax1.set_xlim(-sis.x/2, sis.x/2) ax1.set_ylim(-sis.y/2, sis.y/2) ax2.set_xlim(0, 1200) ax2.set_ylim(0, 50) return scatter, entropia, ani=FuncAnimation(fig, update, frames=range(1200), init_func=init, blit=True, interval=1/30, repeat=False) plt.show()
解决方案
确认VS Code使用的Python解释器
查看VS Code左下角状态栏显示的Python版本/环境,点击即可切换到安装了matplotlib的环境;或者按Ctrl+Shift+P(Windows)/Cmd+Shift+P(Mac),输入Python: Select Interpreter,选择正确的虚拟环境或全局Python解释器。验证matplotlib是否在当前环境安装
在VS Code终端执行python -m pip list,查看输出中是否有matplotlib。如果没有,用当前环境的pip安装:python -m pip install matplotlib(如果是Python3环境,也可以用python3 -m pip install matplotlib)。检查工作区设置的解释器路径
打开工作区的.vscode/settings.json文件,查看是否有python.pythonPath或python.defaultInterpreterPath配置项,确认路径指向的是安装了matplotlib的Python环境。清除VS Code缓存
关闭VS Code,删除以下路径的缓存文件后重启:- Windows:
%APPDATA%\Code\Cache、%APPDATA%\Code\CachedData - Mac:
~/Library/Caches/Code、~/Library/Application Support/Code/CachedData - Linux:
~/.config/Code/Cache、~/.config/Code/CachedData
- Windows:
排查虚拟环境冲突
如果使用了虚拟环境,确保在VS Code终端中已激活正确的环境(Windows:venv\Scripts\activate;Mac/Linux:source venv/bin/activate),再运行代码。
内容的提问来源于stack exchange,提问作者Ftel

