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

问题

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

>>> import matplotlib
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/__init__.py", line 141, in 
    from . import cbook, rcsetup
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/rcsetup.py", line 25, in 
    from matplotlib.fontconfig_pattern import parse_fontconfig_pattern
  File "/home/xiaobo/.local/lib/python3.5/site-packages/matplotlib/fontconfig_pattern.py", line 19, in 
    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 
    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

你可能感兴趣的:(pyparsing,matplotlib,python,python)