psychopy.org | Reference | Downloads | Github

PyPlot Issues Result From Importing Psychopy Modules

I’m not sure if this is the best place to post this, but a colleague and I feel that it is important to at least inform the community in case someone else is experiencing this issue.

When trying to import pyplot, from the following code:

from psychopy import core, visual, gui, data, event, parallel
from psychopy.tools.filetools import fromFile, toFile
import time, random, os, sys, serial, ctypes, itertools
import numpy as np
from PIL import Image, ImageDraw
import copy as copy
from matplotlib import pyplot as plt

I would receive this error:

LookupError                               Traceback (most recent call last)
/Users/Eichenbaum/HWNI/test_HRL_v1_cluster.py in <module>()
      7 from PIL import Image, ImageDraw
      8 import copy as copy
----> 9 from matplotlib import pyplot as plt
     10 
     11 TESTMODE = False

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py in <module>()
     28 import matplotlib
     29 import matplotlib.colorbar
---> 30 from matplotlib import style
     31 from matplotlib import _pylab_helpers, interactive
     32 from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/style/__init__.py in <module>()
      1 from __future__ import absolute_import
      2 
----> 3 from .core import use, context, available, library, reload_library

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/style/core.py in <module>()
    211 # Load style library
    212 # ==================
--> 213 _base_library = load_base_library()
    214 
    215 library = None

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/style/core.py in load_base_library()
    156     """Load style library defined in this package."""
    157     library = dict()
--> 158     library.update(read_style_directory(BASE_LIBRARY_PATH))
    159     return library
    160 

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/style/core.py in read_style_directory(style_dir)
    189     styles = dict()
    190     for path, name in iter_style_files(style_dir):
--> 191         styles[name] = rc_params_from_file(path, use_default_template=False)
    192     return styles
    193 

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/__init__.pyc in rc_params_from_file(fname, fail_on_error, use_default_template)
   1149         parameters specified in the file. (Useful for updating dicts.)
   1150     """
-> 1151     config_from_file = _rc_params_in_file(fname, fail_on_error)
   1152 
   1153     if not use_default_template:

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/__init__.pyc in _rc_params_in_file(fname, fail_on_error)
   1066     cnt = 0
   1067     rc_temp = {}
-> 1068     with _open_file_or_url(fname) as fd:
   1069         try:
   1070             for line in fd:

/Users/Eichenbaum/anaconda/lib/python2.7/contextlib.pyc in __enter__(self)
     15     def __enter__(self):
     16         try:
---> 17             return self.gen.next()
     18         except StopIteration:
     19             raise RuntimeError("generator didn't yield")

/Users/Eichenbaum/anaconda/lib/python2.7/site-packages/matplotlib/__init__.pyc in _open_file_or_url(fname)
   1051         if encoding is None:
   1052             encoding = "utf-8"
-> 1053         with io.open(fname, encoding=encoding) as f:
   1054             yield f
   1055 

LookupError: unknown encoding: 

We tried to uninstall matplotlib and reinstall it. We tried to set up a new environment with new installs of all necessary packages, but we ran into the same error every time.

After somewhat random trial and error, we figured out that if you have from matplotlib import pyplot as the first line in the script (and not having attempted to run the script before in the same python session), the error did not appear and pyplot was successfully imported.

To test it even further, we found that the error was with from psychopy import core, visual, giu, data, event, parallel, such that, if you run from psychopy import <module> individually for each one, you don’t run into the pyplot error. However, only when running from psychopy import <module>, <module> , and specifically visual, gui, does the encoding error appear when attempting to import pyplot. When doing core, gui, no subsequent pyplot importing error occurs, so I’m not sure what exactly the case is, as it’s not just simply having gui imported at the same time (i.e. using commas) as the other modules since we see that interaction with (at least [didn’t test all other interactions]) visual.

This error only occurs if you attempt to import the psychopy modules prior to pyplot, as importing pyplot first fixes the problem. Again, I do not know why this is happening, nor do I know how to fix it other than just avoiding it, so take it for what it is. Hopefully this is helpful to someone.

This is almost certainly a clash between wxPython and matplotlib, rather than something in PsychoPy itself.

The basic issue is to do with libs with threads to do things asynchronously. Applications/windows with event handlers need to set up a thread to keep checking whether buttons are being pressed. PsychoPy’s guy module needs to do that, as does matplotlib. These are clashing in our environment and it’s beyond me to work out how to fix it. Workarounds:

  • changing import order can help (as you found, although I’m not sure why the import syntax you choose would make any difference)
  • changing the backend of matplotlib by doing import matplotlib; matplotlib.use(someOtherOption) before doing import pyplot
1 Like

I was having a similar issue importing pandas (which I see also uses matplotlib), and moving the import pandas statement to the first line of my script worked for me.

Thanks for the fix!