Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 476
AndreyPanferov Avatar asked Apr 27 '26 19:04

AndreyPanferov


1 Answers

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

And in your Dockerfile add:

ENV PYTHONIOENCODING=utf-8

like image 182
Eduardo Soares Avatar answered Apr 30 '26 08:04

Eduardo Soares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!