darkreactions/ESCALATE_Capture

matplotlib error on MacOS

miketynes opened this issue · 0 comments

When running inside of a conda environment (python=3.7.3, built directly from requirements.txt on commit 4b7463d) I encountered the following error:

Traceback (most recent call last):
  File "runme.py", line 9, in <module>
    from capture import specify
  File "/Users/michaeltynes/repos/ESCALATE_Capture/capture/specify.py", line 11, in <module>
    from capture.generate import generator
  File "/Users/michaeltynes/repos/ESCALATE_Capture/capture/generate/generator.py", line 5, in <module>
    from capture.inspect import plotter
  File "/Users/michaeltynes/repos/ESCALATE_Capture/capture/inspect/plotter.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "/Users/michaeltynes/anaconda3/envs/ESCALATE/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2374, in <module>
    switch_backend(rcParams["backend"])
  File "/Users/michaeltynes/anaconda3/envs/ESCALATE/lib/python3.7/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/michaeltynes/anaconda3/envs/ESCALATE/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/michaeltynes/anaconda3/envs/ESCALATE/lib/python3.7/site-packages/matplotlib/backends/backend_macosx.py", line 14, in <module>
    from matplotlib.backends import _macosx

ImportError: 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 have tried two patches, both of which work:

  1. Using pythonw instead of python to run the script. Notably, this is the patch suggested by the error message from matplotlib. This worked for me, but first I had to install python.app with conda install -c anaconda python.app . This seems like a good fix: just note that the install of python.app caused the following package upgrades in the conda environment, which seem entirely inconsequential to me:
     ca-certificates  {2019.1.23 -> 2019.1.23 (anaconda)}
     certifi  {2019.3.9 -> 2019.3.9 (anaconda)}
     openssl  {1.1.1b -> 1.1.1 (anaconda)}
    +python.app-2 (anaconda)
    
  2. Specifying the matplotlib backend by inserting
    import matplotlib
    matplotlib.use('TkAgg')
    
    before running import matplotlib.pyplot as plt.
    This seems messier to me, and I don't fully understand it, so I don't think we should use it.

Note: I got some help from here