导入matplotlib时出现问题

时间:2016-05-28 08:00:54

标签: python matplotlib homebrew osx-yosemite pyenv

我已经建立了一个虚拟环境,可以使用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

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

2 个答案:

答案 0 :(得分:1)

我已经找到了解决方案!似乎我在我的虚拟环境中使用的python版本(2.7.11)已配置为使用unicode=ucs4,因此只需卸载virtualenv / python版本并运行

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

解决了问题!

答案 1 :(得分:-1)

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

我做了

conda install libgcc

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