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

查看:141
本文介绍了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).

在PyCharm在2.7.15上运行的虚拟环境中,无任何投诉地安装了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...)

推荐答案

有关详细信息,请参见:

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文件中的backend参数(请参阅自定义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中的backend参数,即使当前工作目录中存在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天全站免登陆