简体   繁体   中英

How can I run matplotlib in Pycharm on macOS?

I can run code through terminal and have a matplotlib window pop up with the graph but not through Pycharm. All I get is an error saying:

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.

I'm not sure how to install Python as a framework or what that really means. I've googled about but I don't want to mess my system up.

Can someone offer a solution at least of what I should be trying to do or what to google around for.

Sorry if I lack some of the technical language/knowledge to explain this problem. Thanks

  • macOS High Sierra 10.13.1

If you install matplotlib with pip, or conda, you should have a directory ~/.matplotlib .

Inside that directory create, or open if it already exists, a file called matplotlibrc .

To make TkAgg the default backend just add the line backend: TkAgg

I found a short term fix until I can make changes to the necessary files from user Iron Pillow.

import matplotlib matplotlib.use('TkAgg')

I just insert this before I import any matplotlib subpackages.

I encountered a similar situation that a graph report library based on matplotlib works fine on Conda, Jupyter, but not in Pycharm. It can generate a multi-page pdf. But when I run a program that calls the report library inside Pycharm on MacOS, and the pdf can not be generated properly.

My python 3.6 installation is from Anaconda. I found two solutions. First solution is to run pythonw from a terminal. This is obviously outside of Pycharm, and is not as convenient as you wish.

The second solution is to add "backend: TkAgg" to matplotlibrc. This file can exist in several places. If this change is meant to be personal, then this file can live under ~/.matplotlib. You can even add this file in the current working directory, to make it part of the program you run.

This is the least intrusive solution without having to add python code that breaks platform compatibility. Formal documentation can be found in the "The matplotlibrc file" section of https://matplotlib.org/users/customizing.html

You just have to install python. You can do that using Homebrew .

In a terminal run:

sudo brew install python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM