Matplotlib 无法升级的解决方法
阅读更多:Matplotlib 教程
问题描述
在 Ubuntu 14.04 系统下使用 pip 或 conda 管理 Python 包时,经常会遇到 Matplotlib 无法升级的问题。具体表现为:
$ pip install --upgrade matplotlib
...
Installing collected packages: matplotlib
Found existing installation: matplotlib 1.3.1
...
OSError: [Errno 13] Permission denied: '/usr/lib/python2.7/dist-packages/matplotlib/..."
即使使用了 sudo,也无法升级 Matplotlib。问题的原因是 Ubuntu 14.04 版本自带的 Matplotlib 版本较老(通常为 1.3.1),而系统路径和权限的设置使得新版 Matplotlib 安装失败。
解决方法
有多种解决方法可选,本文主要介绍以下两种:
方法一
一种简单的方法是手动卸载当前版本的 Matplotlib,然后再安装新版。
步骤如下:
- 检查当前系统中已安装的 Matplotlib 版本:
$ pip freeze | grep matplotlib matplotlib==1.3.1 - 使用
pip卸载当前版本的 Matplotlib:$ sudo pip uninstall matplotlib如果使用了虚拟环境,请先激活虚拟环境。
-
使用
pip安装最新版本的 Matplotlib:$ sudo pip install matplotlib注意:如果需要使用 PyGTK 或 PyGObject 后端,需要在安装 Matplotlib 之前先安装对应的依赖,比如:
$ sudo apt-get install python-gtk2 $ sudo apt-get install python-gi - 测试 Matplotlib 是否已经成功安装:
import matplotlib print(matplotlib.__version__)
方法二
另一种方法是使用 pip 安装 Matplotlib 的 wheel 包(即已编译好的二进制包),而不需要编译源代码。
步骤如下:
- 下载最新版本的 Matplotlib 的 wheel 包,比如:
“`python
matplotlib-3.4.2-cp27-cp27mu-manylinux1_x86_64.whl
“`
其中 cp27 表示 Python 2.7,cp27mu 表示使用了 GIL(Global Interpreter Lock)优化,manylinux1_x86_64 表示兼容多个 Linux 发行版的 64 位二进制包。
可以从 Matplotlib 的官网或 PyPI 网站下载,也可以使用以下命令从清华镜像站下载:
“`python
$ wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pypi/matplotlib/matplotlib-3.4.2-cp27-cp27mu-manylinux1_x86_64.whl
“`
- 使用
pip安装下载的 wheel 包:
“`python
$ sudo pip install matplotlib-3.4.2-cp27-cp27mu-manylinux1_x86_64.whl
“`
- 测试 Matplotlib 是否已经成功安装:
“`python
$ python -c "import matplotlib; print(matplotlib.__version__)"
“`
总结
本文介绍了两种解决 Ubuntu 14.04 下 Matplotlib 无法升级的方法,分别是手动卸载和安装、使用 wheel 包安装。具体选择哪种方法,可以根据个人喜好和实际情况进行选择。在阅读本文之前,建议先确保系统中已经安装了 pip 和 Python 开发环境。如果还有其他问题,欢迎在评论区留言。
极客教程