【问题标题】:How can I run matplotlib in Pycharm on macOS?如何在 macOS 上的 Pycharm 中运行 matplotlib?
【发布时间】:2018-05-29 19:11:27
【问题描述】:

我可以通过终端运行代码并弹出一个带有图形的 matplotlib 窗口,但不能通过 Pycharm。我得到的只是一个错误提示:

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. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

我不确定如何将 Python 安装为框架或这意味着什么。我已经用谷歌搜索了,但我不想弄乱我的系统。

有人可以提供至少我应该尝试做什么或谷歌搜索的解决方案吗?

对不起,如果我缺乏一些技术语言/知识来解释这个问题。谢谢

  • macOS High Sierra 10.13.1

【问题讨论】:

    标签: python macos matplotlib pycharm


    【解决方案1】:

    我遇到过类似的情况,基于 matplotlib 的图形报告库在 Conda、Jupyter 上运行良好,但在 Pycharm 上却不行。它可以生成多页pdf。但是当我在 MacOS 上运行调用 Pycharm 内部报告库的程序时,无法正确生成 pdf。

    我的 python 3.6 安装来自 Anaconda。我找到了两个解决方案。第一个解决方案是从终端运行 pythonw。这显然在 Pycharm 之外,并没有你希望的那么方便。

    第二种解决方案是在matplotlibrc中添加“后端:TkAgg”。 该文件可以存在于多个位置。如果此更改是针对个人的,则此文件可以位于 ~/.matplotlib 下。您甚至可以将此文件添加到当前工作目录中,使其成为您运行的程序的一部分。

    这是一种侵入性最小的解决方案,无需添加破坏平台兼容性的 python 代码。正式文档可以在https://matplotlib.org/users/customizing.html的“matplotlibrc 文件”部分找到

    【讨论】:

      【解决方案2】:

      如果你使用 pip 或 conda 安装 matplotlib,你应该有一个目录 ~/.matplotlib

      在该目录中创建或打开一个名为matplotlibrc 的文件。

      要使 TkAgg 成为默认后端,只需添加行 backend: TkAgg

      【讨论】:

      • 或者编辑import matplotlib; print(matplotlib.matplotlib_fname())指向的文件。
      • 是的,这是我唯一的工作方式。此官方说明不适用于 conda:matplotlib.org/faq/osx_framework.html
      【解决方案3】:

      我找到了一个短期修复,直到我可以从用户 Iron Pillow 更改必要的文件。

      import matplotlib matplotlib.use('TkAgg')

      我只是在导入任何 matplotlib 子包之前插入它。

      【讨论】:

      • 有没有人知道如何永久实现这一点,而不必每次都使用使用 matplotlib 文件的文件?
      • 编辑import matplotlib; print(matplotlib.matplotlib_fname())输出的文件。有一行写着backend: xxx。改变它。
      【解决方案4】:

      你只需要安装 python。你可以使用Homebrew来做到这一点。

      在终端运行中:

      sudo brew install python

      【讨论】:

      • 感谢您的回复,但我已经在我的主目录和 Pycharm 上安装了 python。有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 2017-01-13
      • 2020-10-18
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多