如何修复在 python3 上导入模块时出现的错误

How to fix errors importing modules on python3

我在 python 上成功导入了模块,但我一直有相同的输出错误 ModuleNotFoundError: No module named '' 错误。

我在 mac 终端中多次安装了 matplotlib、eyeD3 和 mutagen:

#I used this code to install matplotlib on mac terminal
pip install --user  matplotlib

#I used this code to install eyeD3 on mac terminal
pip install eyeD3

当我尝试像下面的代码(或 eyeD3)一样导入 matplotlib 时,我收到了 Traceback 错误。

#to import matplotlib for example
import matplotlib.pyplot as plt 
x = [1,2,3]
y = [2,4,1] 
plt.plot(x, y) 
plt.xlabel('x - axis') 
plt.ylabel('y - axis') 
plt.title('My first graph!') 

plt.show() 

上面列出的所有模块都已正确安装,但是当我导入其中一个模块时,出现相同的常见错误ModuleNotFoundError: No module named ''

我认为您的计算机上至少有 2 个版本的 python。 当你写 pip install matplotlib 时,它会在 python 上安装 matplotlib。 但是当你启动程序时,另一个版本的 python 正在启动。

要修复它: 命令 python -m pip install matplotlib 将在您想要的版本上安装 matplotlib。 如果要用python3.X,就写python3 -m pip install matplotlib 如果您想使用计算机上安装的最新版本,请写入 py -m pip install matplotlib。 您可以为必须安装的每个模块执行此操作。

希望对你有所帮助