Search code examples
pythondjangounicodecyrillic

Once again, UnicodeEncodeError (ascii codec can't encode)


I'm running python 3.6 + gunicorn + django 2.0.5 in docker container with some cyrillic project and that's what I see when I try to log cyrillic strings in console with Django.

'ascii' codec can't encode character '\u0410' in position 0: ordinal not in range(128)

Also this what happens in shell

Python 3.6.5 (default, May  3 2018, 10:08:28) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> :�ириллица

The same time, when i'm running python 3.5 outside docker container, everything is ok:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> Кириллица

Any ideas how to make python 3.6 inside docker work ok with cyrillic strings?


Solution

  • Use # -*- coding: utf-8 -*- in the first line of your python code.

    And in your Dockerfile add:

    ENV PYTHONIOENCODING=utf-8