Back to Top

Set the locale to UTF-8 for Python 3

Updated 9 October 2017

I had been struggling while installing Odoo 11 (Community Edition) on Docker (Ubuntu 16.04),

The error i`m getting are –

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc2 in position 29: ordinal not in range(128)

Not only this, the python print command terminates when trying to print Unicode characters:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters…

I found that Python 3 uses Unicode everywhere, which makes it inconvenient to use in an environment that claims not to support Unicode, such as these Docker images. This issue can be easily fixed by setting up system`s default locale encoding to UTF-8, as:

Start your headless eCommerce
now.
Find out More

PROBLEM: Hit command ‘locale’ , and check the output:

LANG=
LANGUAGE=
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

SOLUTION:
Directly execute these commands:

locale-gen en_US.UTF-8
export LANG=en_US.UTF-8 LANGUAGE=en_US.en LC_ALL=en_US.UTF-8

or, add these lines in your DockerFile:

# Set the locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

Now, Hit command ‘locale’ , and check the output:

LANG=en_US.UTF-8
LANGUAGE=en_US.en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

That`s it…
Thanks for reading this blog !!!

Your opinions, comments and suggestions are important to keep this extension updated and more usefull !!!


. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


5 comments

  • Sunny Yadav
  • Pedro Vagner
    • Pedro Vagner
      • Mohit Chandra
        • Pedro Vagner
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home