Python uses terminal encoding for printing, which has nothing to do with sys.getdefaultencoding() .
Sometimes, environment variables that define a language, such as LANGUAGE , LC_ALL , LC_CTYPE , LANG may not be set, for example, in the environment used by ssh, upstart, Salt, mod_wsgi, crontab, etc. In this case, the C (POSIX) locale is used, which uses the ascii encoding, which results in a UnicodeEncodeError error, since Russian letters are not representable in ascii. In Python 3.7, PEP 540: Forced UTF-8 Runtime Mode is implemented: utf-8 mode is enabled by default for the "C" locale.
Linux server variants can use the default C locale. Desktop Linux installs usually have a utf-8 locale.
The error in the question is related to the Python bug: Python 3 raises Unicode errors with the C locale . The developers decided to follow the ascii encoding from the C locale, even if this is an error in the overwhelming majority of cases, but in Python 3.7 the situation may improve in the default behavior, see PEP 538 - Coercing the legacy C locale to a UTF-8 based locale .
To temporarily change the encoding used, you can define PYTHONIOENCODING :
$ PYTHONIOENCODING=utf-8 python your-script.py
As a more permanent solution, you need to make sure that the utf-8 locale is used in the environment that runs python. It is not necessary to install the Russian locale to print the Russian text. This is the virtue of Unicode, that you can work with many languages at the same time. For example, there is a C.UTF-8 locale.
If locale -a does not show any utf-8 locales, then on Debian systems, install the locales package and select the utf-8 locale to use:
root# aptitude install locales root# dpkg-reconfigure locales
Individually, each user can set up environment variables (see XUBUNTU variables are not saved ), for example:
export LANG=en_US.UTF-8
The first available variable from the list is used: LC_ALL , LC_CTYPE , LANG .
You may need to configure individual programs, such as xterm, Gnome Terminal, screen, to work with utf-8 separately.