import matplotlib 出现“No module named 'pyparsing'“问题

在使用Python 3.5.7和matplotlib 3.0.3时,导入matplotlib遇到'pyparsing'模块缺失的问题。尽管已通过pip安装pyparsing,但可能因版本过低导致问题。尝试升级pyparsing时遇到'cannot import name ‘main’'的错误。通过修改升级命令解决该问题,成功升级pyparsing并能正常导入matplotlib。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

用pip安装完matplotlib,在python中导入matplotlib时出现如下错误

>>> import matplotlib
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/__init__.py", line 141, in <module>
    from . import cbook, rcsetup
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/rcsetup.py", line 25, in <module>
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/fontconfig_pattern.py", line 19, in <module>
    from pyparsing import (Literal, ZeroOrMore, Optional, Regex, StringEnd,
ImportError: No module named 'pyparsing'

但是利用pip list查看安装的库发现已经安装pyparsing库了,经过分析觉得可能是pyparsing版本过低的原因

解决

最开始打算直接再安装一遍

pip install pyparsing

但是由于pyparsing包已经存在,并不能重新安装

Requirement already satisfied: pyparsing in /usr/lib/python3/dist-packages (2.0.3)

只能进行强制升级

sudo pip install --ignore-installed pyparsing

这里出现了一个问题,cannot import name ‘main’

Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

通过这篇博客解决问题

sudo gedit /usr/bin/pip

将原来的

from pip import main
if __name__ == '__main__':
    sys.exit(main())

改为

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

最后再次执行下面命令,成功升级

sudo pip install --ignore-installed pyparsing
WARNING: The directory '/home/xiaobo/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/home/xiaobo/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pyparsing
  Downloading https://files.pythonhosted.org/packages/11/fa/0160cd525c62d7abd076a070ff02b2b94de589f1a9789774f17d7c54058e/pyparsing-2.4.2-py2.py3-none-any.whl (65kB)
     |████████████████████████████████| 71kB 196kB/s 
Installing collected packages: pyparsing
Successfully installed pyparsing-2.4.2

成功导入matplotlib

ps:
python版本 Python 3.5.7
matplotlib版本 matplotlib 3.0.3

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值