Skip to content Skip to sidebar Skip to footer

Python Unicode Decode Error When Importing Matplotlib

I try to use matplotlib in my python script but I got this error in the terminal: Traceback (most recent call last): File 'graphique.py', line 5, in import mat

Solution 1:

The problem is that you have a non-ASCII character in your current working directory.

That actually shouldn't be a problem at all, but it is because of a combination of other things:

  • matplotlib wants to look in your current working directory for a local matplotlibrc file that overrides your default one.
  • Python thinks you're using the C locale instead of the nice UTF-8 locale that Ubuntu 14 should be defaulting to.

So, for a quick workaround, just run your script from a different directory, that has no non-ASCII characters in it.

If you actually want to solve the problem:

  • Make sure you have the latest Ubuntu 14, Anaconda, and matplotlib.
  • From the shell, echo $LANG. The result should be either empty, or something with UTF-8 in it. If not, search AskUbuntu for how to fix that.
  • Make sure your shell and terminal are both set to UTF-8.

From a quick search of the matplotlib issues, this looks like #3516, which looks like it was fixed in #3594, which I think should be in matplotlib 1.4.1+. Also see #3487. So, assuming you $LANG and terminal are correct, and your matplotlib is 1.4.0 or earlier, this is the most likely cause, and updating it (via conda or pip or apt-get or updating Anaconda itself, however you originally installed it) should be the fix.

Or, of course, you can upgrade to Python 3, which will probably either solve the problem, or give you a better error message that tells you exactly what's wrong. (Although, from scanning the issue report, it looks like matplotlib 1.4.0 doesn't have this exact bug in Python 3, only Python 2, as expected… but it may have a related bug…)

Post a Comment for "Python Unicode Decode Error When Importing Matplotlib"