3

I've read through a dozen google entries and other SO questions regarding the same issue but I can't fix it. The example error code is: import matplotlib as plt, but plenty of other things will trigger it.

The whole output is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-20da4deed62d> in <module>()
      1 #import pandas as pd
----> 2 import matplotlib as plt

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py in <module>()
   1129 
   1130 # this is the instance used by the matplotlib classes
-> 1131 rcParams = rc_params()
   1132 
   1133 if rcParams['examples.directory']:

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py in rc_params(fail_on_error)
    973         return ret
    974 
--> 975     return rc_params_from_file(fname, fail_on_error)
    976 
    977 

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py in rc_params_from_file(fname, fail_on_error, use_default_template)
   1098         parameters specified in the file. (Useful for updating dicts.)
   1099     """
-> 1100     config_from_file = _rc_params_in_file(fname, fail_on_error)
   1101 
   1102     if not use_default_template:

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py in _rc_params_in_file(fname, fail_on_error)
   1016     cnt = 0
   1017     rc_temp = {}
-> 1018     with _open_file_or_url(fname) as fd:
   1019         try:
   1020             for line in fd:

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/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")

/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py in _open_file_or_url(fname)
    998     else:
    999         fname = os.path.expanduser(fname)
-> 1000         encoding = locale.getdefaultlocale()[1]
   1001         if encoding is None:
   1002             encoding = "utf-8"

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in getdefaultlocale(envvars)
    541     else:
    542         localename = 'C'
--> 543     return _parse_localename(localename)
    544 
    545 

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in _parse_localename(localename)
    473     elif code == 'C':
    474         return None, None
--> 475     raise ValueError, 'unknown locale: %s' % localename
    476 
    477 def _build_localename(localetuple):

ValueError: unknown locale: UTF-8

This error shows up in a Jupyter Notebook, trying different codes, but so far not in the terminal.


I thought they might be using different binaries but it doesn't look that way running print sys.executable:

  • Notebook: /usr/local/opt/python/bin/python2.7
  • CLI: /usr/local/opt/python/bin/python2.7

Trying to find out more, I run this in the Python CLI:

>>> import locale
>>> print locale.getdefaultlocale()
('en_US', 'UTF-8')

And then in a Notebook:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-35604c361744> in <module>()
      1 import locale
----> 2 print locale.getdefaultlocale()

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in getdefaultlocale(envvars)
    541     else:
    542         localename = 'C'
--> 543     return _parse_localename(localename)
    544 
    545 

/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/locale.pyc in _parse_localename(localename)
    473     elif code == 'C':
    474         return None, None
--> 475     raise ValueError, 'unknown locale: %s' % localename
    476 
    477 def _build_localename(localetuple):

ValueError: unknown locale: UTF-8

print locale.getlocale() on the other hand gets the same output in both places: (None, None)


All I found is that I have to fix my locale settings, but they seem just fine in the Terminal:

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

Edit: little plot twist I just discovered

Using two instances of Jupyter or Ipython makes the whole thing work.

So, whether I first run jupyter notebook or ipython notebook, and then I go and open a new Terminal tab and launch a second jupyter notebook or ipython notebook, that second instance in the browser (or in the Terminal if I don't add the notebook part, I've tried all combinations) will run with no locale problems whatsoever.

In the name of science, I then closed the first instance, and the second instance kept performing just as good.


Edit2: Shell info:

I use Iterm2 running ZSH. At first I tried adding these two lines:

$ head .zshrc
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

If I comment those out and relaunch the console, this is what I get:

$ locale
LANG=
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

In which case, even the Python CLI will throw the same error.

Regarding tabs 1 and 2, I compared results using {locale & locale -a } > tab1 and {locale & locale -a } > tab2 but results are identical in that regard:

enter image description here

And yes down the line I've got these:

en_US
en_US.ISO8859-1
en_US.ISO8859-15
en_US.US-ASCII
en_US.UTF-8

So completeley lost here. How to fix this madness?

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
  • It sounds like the first terminal is running `.bash_profile` or `.profile`, whereas the second will not. Check if anything is declared in there. On your first terminal, run `locale` and compare it to `locale` on the second. `LC_ALL` isn't usually set, so track down where that's being declared. Also, run `locale -a` and make sure `en_US.UTF-8` is listed – Alastair McCormack Jun 26 '16 at 11:10
  • Thanks for helping out, I've updated the post with that info. – Neithan Max Jun 28 '16 at 08:59
  • I assume you're on OS X by the `Cellar` being in your Python path. `$LANG`, which is required to set your locale, is set by the terminal, not by your login scripts. Therefore, I assume that you've either disabled that feature in iTerm or you change user and lose your environment. Anyway, the quick answer is to just set `LANG=en_US.UTF-8` in your `.zshrc` – Alastair McCormack Jun 28 '16 at 09:13
  • echo $LANG returns en_US.UTF-8. Now the first jupyter i just launched worked well, then I killed it and within the same tab, the second one went down with exactly the same error... I've tried though disabling iTerm's "Set locale variables automatically", and so far so good. If it sticks, I'll let you know so you get a chance at replying a solution. – Neithan Max Jun 28 '16 at 12:14

0 Answers0