Mac OS Pycharm 上的 Python 通过“import matplotlib.pyplot as plt"给出框架错误. [英] Python on Mac OS Pycharm gives framework error with "import matplotlib.pyplot as plt"

查看:41
本文介绍了Mac OS Pycharm 上的 Python 通过“import matplotlib.pyplot as plt"给出框架错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于 这里这里但是没有解决方案可能是因为我使用了不同的环境(Mac OS 上的 PyCharm).

This question is similar to the ones here and here but none of the solutions there work perhaps because I´m using a different environment (PyCharm on Mac OS).

在2.7.15上运行PyCharm的虚拟环境中,安装matplotlib没有任何抱怨,一个一行的PyCharm python文件,内容如下...

In a virtual environment with PyCharm running on 2.7.15, matplotlib installed without any complaints, and a one-line PyCharm python file with the following content...

import matplotlib.pyplot as plt

...运行这个单行文件时控制台输出如下错误:

...the console outputs the following error when running this one-line file:

/Users/jbs/PycharmProjects/WakeUp/env/bin/python /Users/jbs/PycharmProjects/WakeUp/InputSound/WakeInputSound-and-plot-it-trial3.py
Traceback (most recent call last):
  File "/Users/jbs/PycharmProjects/WakeUp/InputSound/WakeInputSound-and-plot-it-trial3.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "/Users/jbs/PycharmProjects/WakeUp/env/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/jbs/PycharmProjects/WakeUp/env/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
  File "/Users/jbs/PycharmProjects/WakeUp/env/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 

任何解决此问题的帮助都将受到欢迎.我已经尝试了大约十几件事,但似乎没有任何工作......

Any help resolving this would be most welcomed. I´ve tried about a dozen things and none seems to work...

(单独导入matplotlib不会出现问题,这个问题可能与这个一个,但它显然不同......)

(import matplotlib alone does not give problems and this question may be related to this one but it´s clearly different...)

推荐答案

详情参考:什么是后端.你需要设置你的后端.有两种类型的后端:用户界面后端(用于 pygtk、wxpython、tkinter、qt4 或 macosx;也称为交互式后端")和用于制作图像文件的硬拷贝后端(PNG、SVG、PDF、PS;也称为非交互式后端").

For details refer: what-is-a-backend. You need to set your backend. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx; also referred to as "interactive backends") and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as "non-interactive backends").

有四种方法可以配置您的后端.如果它们相互冲突,将使用以下列表中最后提到的方法,例如调用 use() 将覆盖 matplotlibrc 中的设置.

There are four ways to configure your backend. If they conflict each other, the method mentioned last in the following list will be used, e.g. calling use() will override the setting in your matplotlibrc.

  1. matplotlibrc 文件中的后端参数(参见自定义 matplotlib):

  1. The backend parameter in your matplotlibrc file (see Customizing matplotlib):

backend : WXAgg   # use wxpython with antigrain (agg) rendering

  • 为当前的 shell 或单个脚本设置 MPLBACKEND 环境变量:

  • Setting the MPLBACKEND environment variable, either for your current shell or for a single script:

    > export MPLBACKEND="module://my_backend"
    > python simple_plot.py
    
    > MPLBACKEND="module://my_backend" python simple_plot.py
    

    设置此环境变量将覆盖任何 matplotlibrc 中的后端参数,即使您当前的工作目录中有 matplotlibrc.因此全局设置 MPLBACKEND,例如在您的 .bashrc 或 .profile 中,不鼓励这样做,因为它可能会导致违反直觉的行为.

    Setting this environment variable will override the backend parameter in any matplotlibrc, even if there is a matplotlibrc in your current working directory. Therefore setting MPLBACKEND globally, e.g. in your .bashrc or .profile, is discouraged as it might lead to counter-intuitive behavior.

    要为单个脚本设置后端,您也可以使用 -d 命令行参数:

    To set the backend for a single script, you can alternatively use the -d command line argument:

    > python script.py -dbackend
    

    此方法已弃用,因为 -d 参数可能与解析命令行参数的脚本冲突(请参阅问题 #1986).您应该改用 MPLBACKEND.

    This method is deprecated as the -d argument might conflict with scripts which parse command line arguments (see issue #1986). You should use MPLBACKEND instead.

    如果您的脚本依赖于特定的后端,您可以使用 use() 函数:

    If your script depends on a specific backend you can use the use() function:

    import matplotlib
    matplotlib.use('PS')   # generate postscript output by default
    

    如果你使用 use() 函数,这必须在导入 matplotlib.pyplot 之前完成.在导入 pyplot 后调用 use() 将无效.如果用户想要使用不同的后端,使用 use() 将需要更改您的代码.因此,除非绝对必要,否则应避免显式调用 use().

    If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect. Using use() will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use() unless absolutely necessary.

    注意:后端名称规范不区分大小写;例如,GTKAgg"和gtkagg"是等价的.

    这篇关于Mac OS Pycharm 上的 Python 通过“import matplotlib.pyplot as plt"给出框架错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆