Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    Set the locale to UTF-8 for Python 3

    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:

    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 !!!

    
    
    . . .

    Comments (4)

    Add Your Comment

  • Pedro Vagner
    Thank you for this post. But it doesn’t solve my problem.
    • Pedro Vagner
      I just solved my problem:

      RUN sed -i -e ‘s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/’ /etc/locale.gen &&
      sed -i -e ‘s/# pt_BR.UTF-8 UTF-8/pt_BR.UTF-8 UTF-8/’ /etc/locale.gen &&
      locale-gen

      • Mohit Chandra
        Hi,
        I have tested this piece of code on Ubuntu (Xenial) only. Are you doing it on Ubuntu or Debian or any other os. Please confirm, so that i can update my blog as well 🙂
        Thanks for your feedback.
        • Pedro Vagner
          I’m using the official Odoo 11 Docker HUB (hub.docker.com/_/odoo/). It uses debian:stretch. It doesn’t include locale-gen utility. Here is the complete Docker snippet code:

          RUN set -x; apt-get update &&
          DEBIAN_FRONTEND=noninteractive apt-get install -y –no-install-recommends locales

          RUN sed -i -e ‘s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/’ /etc/locale.gen &&
          sed -i -e ‘s/# pt_BR.UTF-8 UTF-8/pt_BR.UTF-8 UTF-8/’ /etc/locale.gen &&
          locale-gen

          ENV LANG en_US.UTF-8
          ENV LANGUAGE en_US.en
          ENV LC_ALL en_US.UTF-8

  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close