1

I'm trying learning to use encoding declarations in source files reading PEP 263 and I'm experimenting on my own but I got some troubles.

Here's my file cod.py:

# -*- coding: utf-16 -*-
print('ciao')

and I saved it using UTF-16 encoding; now:

antox@antox-pc ~/Scrivania $ python3 cod.py 
  File "cod.py", line 1
SyntaxError: Non-UTF-8 code starting with '\xff' in file cod.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

So I don't understand where I'm getting wrong.

P.S. I'm using gedit 2.30.4

zer0uno
  • 7,521
  • 13
  • 57
  • 86

1 Answers1

0

UTF-16 is not accepted as encoding for Python source code. From PEP 263 (section Concepts, item 1):

Any encoding which allows processing the first two lines in the way indicated above is allowed as source code encoding, this includes ASCII compatible encodings as well as certain multi-byte encodings such as Shift_JIS. It does not include encodings which use two or more bytes for all characters like e.g. UTF-16. The reason for this is to keep the encoding detection algorithm in the tokenizer simple.

So the error you're getting is expected: you can use a different encoding (other than the default UTF-8) as long as it can be detected by Python.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • See also: http://stackoverflow.com/questions/26132121/which-file-encodings-are-supported-for-python-3-source-files – Simeon Visser Jan 28 '15 at 23:57
  • One thing I don't understand, why should someone use encoding declaration? Python 3.X defaults to utf-8 and I think that today everyone uses this encoding in their file – zer0uno Jan 29 '15 at 00:01
  • @antox: yes, if you use UTF-8 then you don't need to declare the encoding. It's only when you want it to be different (for some reason). – Simeon Visser Jan 29 '15 at 00:02
  • @simeon_visser I would ask you one thing, did this problem of declaring encoding come out because in the previous python versions the default was ascii and before python 2.1 it wasn't accepted another encoding other than ascii? – zer0uno Jan 29 '15 at 00:45
  • @antox: that's possible - I wasn't using Python when it was 2.1 but I do know that understanding of encodings came later with Python's growing use and the release of Python 3.x (and based on usage such as web frameworks separating bytes and Unicode with 2.x). Source encodings are useful but UTF-8 should work for most - as the PEP says this change was made due to the situation in Python 2.1. It's only now that UTF-8 is the default and that you don't really need to declare a different encoding. – Simeon Visser Jan 29 '15 at 00:52