【问题标题】:AttributeError: 'module' object has no attribute 'style'AttributeError:“模块”对象没有属性“样式”
【发布时间】:2016-11-22 14:34:30
【问题描述】:

我使用的是 Mac,

我尝试使用ggplot如下

import matplotlib.pyplot as plt
plt.style.use('ggplot')

但我总是收到类似

的错误
File "/Users/Desktop/code.py", line 4, in <module>
    plt.style.use('ggplot')
AttributeError: 'module' object has no attribute 'style'

我一直在寻找解决方案,但我不知道我在哪里犯了错误。有什么意见吗?

如前所述,我必须升级matplotlib然而,我得到了错误 我的python版本是2.7 所以我尝试了这个

pip2 install matplotlib

这里是错误

Requirement already satisfied: matplotlib in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requirement already satisfied: numpy>=1.5 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: tornado in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: pyparsing>=1.5.6 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: nose in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: singledispatch in /Library/Python/2.7/site-packages/singledispatch-3.4.0.3-py2.7.egg (from tornado->matplotlib)
Requirement already satisfied: certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: backports_abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado->matplotlib)

或者我试过了

sudo port install py27-pip

这里是错误

Password:
Error: Current platform "darwin 16" does not match expected platform "darwin 15"
Error: If you upgraded your OS, please follow the migration instructions: https://trac.macports.org/wiki/Migration
OS platform mismatch
    while executing
"mportinit ui_options global_options global_variations"
Error: /opt/local/bin/port: Failed to initialize MacPorts, OS platform mismatch

或者我试过这个

python get-pip.py
#Requirement already up-to-date: pip in /Library/Python/2.7/site-packages

然后

pip install matplotlib

错误如下

Requirement already satisfied: matplotlib in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Requirement already satisfied: numpy>=1.5 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: tornado in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: pyparsing>=1.5.6 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib)
Requirement already satisfied: nose in /Library/Python/2.7/site-packages (from matplotlib)
Requirement already satisfied: singledispatch in /Library/Python/2.7/site-packages/singledispatch-3.4.0.3-py2.7.egg (from tornado->matplotlib)
Requirement already satisfied: certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: backports_abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib)
Requirement already satisfied: six in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from singledispatch->tornado->matplotlib)


pip2 install -U matplotlib

sudo pip2 install -U matplotlib

它给了我很多错误,只是底部爆炸了几行

('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo', '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo', "[Errno 1] Operation not permitted: '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz/zoneinfo'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz', '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz', "[Errno 1] Operation not permitted: '/var/folders/0y/736q6sd901z27bmpx9nwn_p40000gn/T/pip-r5YiwW-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz'")]

如果我对sudo 做同样的事情

"[Errno 1] Operation not permitted: '/tmp/pip-5PMqIK-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pytz'")]

【问题讨论】:

标签: python matplotlib module


【解决方案1】:

一种方法是做

sudo pip2 install -U matplotlib --ignore-installed six

您必须使用 --ignore-installed 6,因为 OS 附带了 6 个 1.4.1 安装并且如果尝试卸载它(因为 awscli 依赖于 botocore,botocore 依赖于 python -dateutil 和 python-dateutil 依赖于六个 >= 1.5) 它没有权限这样做,因为系统完整性保护甚至不允许 root 修改这些目录。

此处突出显示https://github.com/pypa/pip/issues/3165

完成此操作后,您可以检查matplotlibas 的版本

python -c 'import matplotlib; print matplotlib.__version__'

如果你得到你提到的错误

/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

,那么就很容易解决了

打开终端

然后写python

然后

>>> import matplotlib as mpl
>>> mpl.get_configdir()

知道路径后,转到finder -----> Go 在那里搜索文件夹并删除fontList

然后再次运行您的程序,您将再次收到警告并解决。 更多信息,也可以在这里找到第二部分 matplotlib taking time when being imported

【讨论】:

    猜你喜欢
    • 2010-11-18
    • 2018-08-28
    相关资源
    最近更新 更多