ImportError:没有名为'matplotlib.externals'的模块

时间:2017-07-14 12:40:29

标签: python ubuntu matplotlib seaborn

当我在Ubuntu 16.04上使用python3中的seaborn.heatmap运行file1.py时,

我收到错误消息:

Traceback (most recent call last):
  File "file1.py", line 19, in <module>
    import seaborn as sns
ImportError: No module named 'seaborn'

然后我在终端中运行命令$ sudo apt install python3-seaborn

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libdsdp-5.8gf libglade2-0 libglpk36 python-cvxopt python-cycler
  python-decorator python-glade2 python-imaging python-jdcal python-joblib
  python-numexpr python-openpyxl python-pandas python-pandas-lib python-patsy
  python-py python-pytest python-scipy python-statsmodels
  python-statsmodels-lib python-tables python-tables-lib
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  python3-matplotlib
Suggested packages:
  ffmpeg ipython3 python-matplotlib-doc python3-cairocffi python3-gobject
  python3-nose python3-pyqt4 python3-sip python3-tornado ttf-staypuft
The following NEW packages will be installed:
  python3-matplotlib python3-seaborn
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/3,999 kB of archives.
After this operation, 13.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Selecting previously unselected package python3-matplotlib.
(Reading database ... 378283 files and directories currently installed.)
Preparing to unpack .../python3-matplotlib_1.5.1-1ubuntu1_amd64.deb ...
Unpacking python3-matplotlib (1.5.1-1ubuntu1) ...
Selecting previously unselected package python3-seaborn.
Preparing to unpack .../python3-seaborn_0.6.0-1_all.deb ...
Unpacking python3-seaborn (0.6.0-1) ...
Setting up python3-matplotlib (1.5.1-1ubuntu1) ...
Setting up python3-seaborn (0.6.0-1) ...

现在file1.py可以成功运行,而我在python3中使用Axes3D运行file2.py, 我收到错误消息:

Traceback (most recent call last):
  File "file2.py", line 29, in <module>
    from mpl_toolkits.mplot3d import Axes3D
  File "/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/__init__.py", line 4, in <module>
    from matplotlib.externals import six
ImportError: No module named 'matplotlib.externals'

只有我运行$ sudo apt remove python3-matplotlib才能运行file2.py:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  fonts-lyx libjs-jquery-ui python-matplotlib-data python-tables-data python3-cycler
  python3-dateutil python3-decorator python3-numexpr python3-pandas python3-pandas-lib python3-patsy
  python3-scipy python3-tables python3-tables-lib python3-tz
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  python3-matplotlib python3-seaborn
0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded.
After this operation, 13.8 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 375396 files and directories currently installed.)
Removing python3-seaborn (0.6.0-1) ...
Removing python3-matplotlib (1.5.1-1ubuntu1) ...

然而,python3-seaborn也同时被删除。我怎么解决这个问题?提前谢谢!

3 个答案:

答案 0 :(得分:5)

sudo apt-get remove python3-matplotlib

UPD: 然后使用传统的pip - 方式安装它: sudo pip3 install matplotlib

此外,@ Andy已注意到该脚本不仅会删除matplotlib,还会删除seaborn。因此,您可能还需要使用pip安装它。

我试图做@ Bodhi94建议的但是没有成功。然后我尝试从Ubuntu中删除python3-matplotlib,结果就算了。

是否可能出现任何冲突?请允许我,如果有的话。

答案 1 :(得分:4)

尝试使用以下命令重新安装matplotlib。

 sudo pip install matplotlib --upgrade --ignore-installed six

答案 2 :(得分:2)

我在所有相关的Python文件中替换如下: 我收到消息后立即 ImportError:没有名为'matplotlib.externals'的模块

我做了: -sudo nano /usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/init.py - 并且我重新说:“从matplotlib.externals导入六个”通过“import six”

直到剩下错误。

从那以后工作完美