IPython Notebook locale error

49,831

Solution 1

I summarize here the solution to be found on: http://blog.lobraun.de/2009/04/11/mercurial-on-mac-os-x-valueerror-unknown-locale-utf-8/

I added these lines to my .bash_profile:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

I reloaded the profile:

source ~/.bash_profile

I then ran ipython again:

ipython notebook

Changing locales

The above will work for the English language in a US locale. One may want different settings.

At the risk of stating the obvious, to discover the current settings for your system, use:

$ locale

And to retrieve a list of all valid settings on your system:

$ locale -a

Then choose your preferred locale. For example, for a Swiss French locale, the solution would look like this:

export LC_ALL=fr_CH.UTF-8
export LANG=fr_CH.UTF-8

Solution 2

This is a bug in the OS X Terminal app that only shows up in certain locales (country/language combinations). Open Terminal in /Applications/Utilities and uncheck the box “Set locale environment variables on startup”.

enter image description here

This will set your LANG environment variable to be empty. This may cause terminal use to incorrect settings for your locale. The locale command in the Terminal will tell you what settings are used. To use the correct language, add a line to your bash profile (typically ~/.profile)

export LANG=your-lang

Replace your-lang with the correct locale specifier for your language. The command locale -a will show you all the specifiers. For example, the language code for US English is en_US.UTF-8. The locale affects what translations are used when they are available, and also how dates, currencies, and decimals are formatted.

Note, this image and content were taken from http://conda.pydata.org/docs/troubleshooting.html#unknown-locale (I'm also the original author of that page).

Solution 3

in iTerm going to the menu

Preferences -> Profiles -> Terminal -> (Environment) 

and then unchecking

"Set locale variables automatically"

made this error go away.

Solution 4

As your LC_CTYPE is wrong, you should find out where that wrong value is set and change it to something like en_US.UTF-8.

Share:
49,831

Related videos on Youtube

Clayton
Author by

Clayton

Building Energy Efficiency Fanatic

Updated on July 09, 2022

Comments

  • Clayton
    Clayton almost 2 years

    After installing the latest Mac OSX 64-bit Anaconda Python distribution, I keep getting a ValueError when trying to start the IPython Notebook.

    Starting ipython works fine:

    3-millerc-~:ipython
    Python 2.7.3 |Anaconda 1.4.0 (x86_64)| (default, Feb 25 2013, 18:45:56) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 0.13.1 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    

    But starting ipython notebook:

    4-millerc-~:ipython notebook
    

    Results in the ValueError (with traceback):

    Traceback (most recent call last):
      File "/Users/millerc/anaconda/bin/ipython", line 7, in <module>
        launch_new_instance()
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 388, in launch_new_instance
        app.initialize()
      File "<string>", line 2, in initialize
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
        return method(app, *args, **kwargs)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 313, in initialize
        super(TerminalIPythonApp, self).initialize(argv)
      File "<string>", line 2, in initialize
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
        return method(app, *args, **kwargs)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/core/application.py", line 325, in initialize
        self.parse_command_line(argv)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 308, in parse_command_line
        return super(TerminalIPythonApp, self).parse_command_line(argv)
      File "<string>", line 2, in parse_command_line
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
        return method(app, *args, **kwargs)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 420, in parse_command_line
        return self.initialize_subcommand(subc, subargv)
      File "<string>", line 2, in initialize_subcommand
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
        return method(app, *args, **kwargs)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/config/application.py", line 352, in initialize_subcommand
        subapp = import_item(subapp)
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/utils/importstring.py", line 40, in import_item
        module = __import__(package,fromlist=[obj])
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/frontend/html/notebook/notebookapp.py", line 46, in <module>
        from .handlers import (LoginHandler, LogoutHandler,
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/IPython/frontend/html/notebook/handlers.py", line 36, in <module>
        from docutils.core import publish_string
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/docutils/core.py", line 20, in <module>
        from docutils import frontend, io, utils, readers, writers
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/docutils/frontend.py", line 41, in <module>
        import docutils.utils
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/docutils/utils/__init__.py", line 20, in <module>
        from docutils.io import FileOutput
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/docutils/io.py", line 18, in <module>
        from docutils.utils.error_reporting import locale_encoding, ErrorString, ErrorOutput
      File "/Users/millerc/anaconda/lib/python2.7/site-packages/docutils/utils/error_reporting.py", line 47, in <module>
        locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
      File "/Users/millerc/anaconda/lib/python2.7/locale.py", line 503, in getdefaultlocale
        return _parse_localename(localename)
      File "/Users/millerc/anaconda/lib/python2.7/locale.py", line 435, in _parse_localename
        raise ValueError, 'unknown locale: %s' % localename
    ValueError: unknown locale: UTF-8
    

    Running the locale command from the terminal:

    5-millerc-~:locale 
    LANG=
    LC_COLLATE="C"
    LC_CTYPE="UTF-8"
    LC_MESSAGES="C"
    LC_MONETARY="C"
    LC_NUMERIC="C"
    LC_TIME="C"
    LC_ALL=
    
    • wRAR
      wRAR about 11 years
      LC_CTYPE="UTF-8" seems wrong to me.
    • Clayton
      Clayton about 11 years
      I changed the question to be platform neutral since I strangely started having the same problem with my EPD python installation as well.
    • Clayton
      Clayton almost 6 years
      @ubuntu - how is this a duplicate if I asked it first?
  • wRAR
    wRAR about 11 years
    Exporting LC_ALL is usually wrong.
  • asmeurer
    asmeurer over 10 years
    Of course, this fix is only correct if you live in the United States.
  • r12
    r12 almost 9 years
    Your link appears to be broken.
  • Linus Unnebäck
    Linus Unnebäck over 8 years
    Full circle! That link you provided links to a forum which then links back to this question :)
  • asmeurer
    asmeurer over 8 years
    There's a more descriptive explanation of this solution at conda.pydata.org/docs/troubleshooting.html#unknown-locale.
  • asmeurer
    asmeurer over 8 years
    That doc page was much more helpful than what I have here, and also the link to that bug was useless, so I just updated my answer with the contents of those docs. Just unchecking the box is generally not enough. You need to set your LANG environment variable to the correct value for your locale.
  • Linus Unnebäck
    Linus Unnebäck over 8 years
    Had to revert this fix since it broke oh-my-zsh 😭
  • asmeurer
    asmeurer over 8 years
    @LinusUnnebäck did you set LANG? It needs to be set to a valid locale for many things to work correctly (see my updates to this answer).
  • alemol
    alemol almost 8 years
    It works for Spanish also: export LC_ALL=es_ES.UTF-8; export LANG=es_ES.UTF-8
  • Alexey
    Alexey almost 8 years
    I do not think that this is a bug in the Terminal app: iTerm2 has the same issue.
  • asmeurer
    asmeurer almost 8 years
    @Alexey I haven't heard of iTerm having this problem. What version are you using?
  • Alexey
    Alexey almost 8 years
    iTerm2 Build 2.1.4
  • Alexey
    Alexey almost 8 years
    The output of locale in Terminal and iTerm2 is identical (when locale variables are set on startup), in particular, in both i have LC_CTYPE="UTF-8".
  • asmeurer
    asmeurer almost 8 years
    Does the iTerm2 beta do better? A lot of things have been fixed with it.
  • uchuugaka
    uchuugaka over 7 years
    @wRAR do you have any links to references on when and when not to export LC_ALL ?? I'd love to know and it would help anybody.
  • wRAR
    wRAR over 7 years
  • uchuugaka
    uchuugaka over 7 years
    Thanks, I found a good deal more on it. I'm still mystified why macOS fails it on some region+language settings. Frustrating.
  • Kshitij Saraogi
    Kshitij Saraogi almost 7 years
    Note: This works for iTerm2 as well.
  • Sealander
    Sealander almost 7 years
    This was obvious in retrospect, but make sure you source the profile in the terminal window where you run Jupyter/iPython...