导入 matplotlib 时出现问题

Issue when importing matplotlib

我已经设置了一个虚拟环境,用于使用 pyenv 进行机器学习。当我 运行 import matplotlib.pyplot as plt 这是我得到的:

ImportError                               Traceback (most recent call last)
<ipython-input-1-eff513f636fd> in <module>()
----> 1 import matplotlib.pyplot as plt

/usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     27 from cycler import cycler
     28 import matplotlib
---> 29 import matplotlib.colorbar
     30 from matplotlib import style
     31 from matplotlib import _pylab_helpers, interactive

/usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/colorbar.py in <module>()
     30
     31 import matplotlib as mpl
---> 32 import matplotlib.artist as martist
     33 import matplotlib.cbook as cbook
     34 import matplotlib.collections as collections

/usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/artist.py in <module>()
     12 from matplotlib.cbook import mplDeprecation
     13 from matplotlib import docstring, rcParams
---> 14 from .transforms import (Bbox, IdentityTransform, TransformedBbox,
     15                          TransformedPath, Transform)
     16 from .path import Path

/usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/transforms.py in <module>()
     37 import numpy as np
     38 from numpy import ma
---> 39 from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,
     40     update_path_extents)
     41 from numpy.linalg import inv

ImportError: dlopen(/usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/_path.so, 2): Symbol not found: _PyUnicodeUCS2_AsASCIIString
  Referenced from: /usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/_path.so
  Expected in: flat namespace
 in /usr/local/var/pyenv/versions/2.7.11/envs/machine_learning/lib/python2.7/site-packages/matplotlib/_path.so

我在 OS X Yosemite,这是我通过 pip 安装的软件包库:

appnope==0.1.0
backports.shutil-get-terminal-size==1.0.0
cffi==1.6.0
cryptography==1.3.2
cycler==0.10.0
decorator==4.0.9
enum34==1.1.6
gnureadline==6.3.3
idna==2.1
inflection==0.3.1
ipaddress==1.0.16
ipython==4.2.0
ipython-genutils==0.1.0
matplotlib==1.5.1
more-itertools==2.2
mpmath==0.19
ndg-httpsclient==0.4.0
nose==1.3.7
numpy==1.11.0
pandas==0.18.1
pathlib2==2.1.0
pexpect==4.0.1
pickleshare==0.7.2
ptyprocess==0.5.1
pyasn1==0.1.9
pycparser==2.14
pyOpenSSL==16.0.0
pyparsing==2.1.4
python-dateutil==2.5.3
pytz==2016.4
Quandl==3.0.0
readline==6.2.4.1
requests==2.10.0
scikit-learn==0.17.1
scipy==0.17.1
simplegeneric==0.8.1
six==1.10.0
sympy==1.0
traitlets==4.2.1

有解决这个问题的方法吗?

我找到解决办法了!我在虚拟环境中使用的 python 版本 (2.7.11) 似乎是为了使用 unicode=ucs4 而配置的,所以只需卸载 virtualenv/python 版本和 [=19] =]

PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs2" pyenv install 2.7.11

问题已解决!

对于那些在特殊环境下使用或不使用python3的人,我发现conda的人一直在解决这个问题。对我有用的是 https://github.com/conda-forge/matplotlib-feedstock/issues/32

我做到了

conda install libgcc

现在我的导入不会引发错误。