0

When I am using Python 2.7 and trying to declare Python source code encoding by:

#!/usr/bin/python
# -*- coding: utf-8 -*-

When I am checking file encoding by:

check_encoding = sys.getdefaultencoding()
logger.warning("check_encoding ")

I am getting:

ascii

When I am using Python 3 I have no problem with that by declaring source code encoding by:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

When I am checking file encoding by:

check_encoding = sys.getdefaultencoding()
logger.warning("check_encoding ")

I am getting:

utf-8

I have no idea what's wrong with Python 2.7 version and what can cause it.

fueggit
  • 859
  • 1
  • 22
  • 44
  • The encoding of your Python script and the encoding used by I/O functions such as `read()` or `input()` are two completely different and unrelated things. – tripleee Sep 25 '18 at 07:59
  • 1
    Possible duplicate of [Why encoding in utf-8 still results in ascii?](https://stackoverflow.com/questions/48413726/why-encoding-in-utf-8-still-results-in-ascii) – tripleee Sep 25 '18 at 08:00

1 Answers1

0

You are confusing the source code encoding (declared with # -*- coding: utf-8 -*) and the default run time encoding with is used for implicit conversions between unicode and byte strings. They are totally unrelated in both Python 2 and Python 3.

In that sense # -*- coding: utf-8 -* behaves exactly the same in Python 2 and Python 3: it declares the encoding of unicode litterals in the current source file.

But the system default encoding is different. It is normally ascii in Python 2 and can depend on environment on Python 3 with a default of UTF-8. So you get the same value in your code only by accident...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252