You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook Pro安装matplotlib后运行代码遇FileNotFoundError求助

问题:运行Matplotlib代码时遭遇FileNotFoundError

我已经在MacBook Pro上用pip3 install装好了matplotlib,试着运行这段代码:

import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig("test.png")
plt.show()

结果弹出了这个错误:

runfile('/Users/giridhar/PycharmProjects/numpy/matpotlib1.py', wdir='/Users/giridhar/PycharmProjects/numpy')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
  File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 11, in execfile
    stream = tokenize.open(file) # @UndefinedVariable
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tokenize.py", line 392, in open
    buffer = _builtin_open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/Users/giridhar/PycharmProjects/numpy/matpotlib1.py'

请问我哪里操作错了?


解决方案

兄弟,这个问题一眼就能看出来——你文件名拼错了!

看错误信息里的路径:matpotlib1.py,但正确的应该是matplotlib1.py(注意中间是字母l,不是p)。PyCharm按照你输入的错误文件名去找,自然找不到对应的文件,就抛出了FileNotFoundError。

给你几个具体的修复步骤:

  • 先去你的项目目录/Users/giridhar/PycharmProjects/numpy/里,找到你的代码文件,把文件名改成matplotlib1.py
  • 在PyCharm里,直接右键点击这个正确命名的文件,选择「Run 'matplotlib1.py'」,别自己手动敲runfile命令(手动输入很容易拼错)
  • 如果还是有问题,确认一下这个文件确实在当前PyCharm打开的项目目录下,有时候项目结构没刷新也会出问题,你可以右键项目根目录选「Reload from Disk」

另外提个小建议:以后保存代码文件的时候,尽量和库名保持一致的拼写,像matplotlib这种常用库,多打几次就记住了,避免这种低级错误。

内容的提问来源于stack exchange,提问作者Giridhar Tirumalai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:08:10