UnicodeEncodeError: 'ascii' codec can't encode character


UnicodeEncodeError: 'ascii' codec can't encode character '\U0001f6d1' in position 2: ordinal not in range(128)

This Python error might happen if you are having problems displaying non-ASCII characters in your terminal when running a Python script. This is mainly a problem with your locale settings used by your terminal. I provide two solutions to resolve it:

  • Solution #1: change your locale settings (best solution)
  • Solution #2: export PYTHONIOENCODING=utf8 (temporary solution)

Solution #1: change your locale settings (best solution)

The best solution consists in fixing your locale settings since it is permanent and you don’t have to change any Python code.
  1. Append ~/.bashrc or ~/.bash_profile with:
    export LANG="en_US.UTF-8"
    export LANGUAGE="en_US:en"
    

    You should provide your own UTF-8 based locale settings. The example uses the English (US) locale with the encoding UTF-8. The locale -a command gives you all the available locales on your Linux or Unix-like system.

  2. Reload the .bashrc:
    $ source .bashrc
    

  3. Run the locale command to make sure that your locale settings were set correctly:
    $ 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=
    

  4. Run your Python script to test if you can display the non-ASCII characters fine using the correct encoding UTF-8 :
    $ python your_script.py
    
See also:

Solution #2: export PYTHONIOENCODING=utf8 (temporary solution)

Before running your Python script, export the environment variable PYTHONIOENCODING with the correct encoding:
$ export PYTHONIOENCODING=utf8
$ python your_script.py

However, this is not a permanent solution because if you use another terminal, you will have to export PYTHONIOENCODING again before running the script.

References


Comments

Popular posts from this blog

Install the MAMP (Mac, Apache, MySQL, PHP) stack

Deactivate conda's base environment on startup

Product review: SMONET wireless security camera system