0

I am a python beginner, trying to run from power shell on vista.

when trying to call a simple script with:

python vc.py

gives error: "File "vcpy", line 1 syntaxError: Non-UTF-8 code starting with '\xff'

... where vc.py is: import sys print sys.version

It does work when I invoke instead:

cat vc.py | python

The problem with this latter approach is that it is giving us problems with the raw input function.

  • 1
    The indicated "duplicate" question does not address this issue *at all*. The linked question (and hundreds other similar) describe how to use the "# coding:" comment to indicate coding to the interpreter, but this error is thrown in response to the FIRST BYTE of the source file, before such a comment is even reached! – aldo Sep 30 '14 at 23:05
  • Ignore the supposed "duplicate" question linked above. The problem is the encoding of the source file itself (probably UTF-16 or some such). See this question and its answer: http://stackoverflow.com/q/26132121/1193893 – aldo Oct 01 '14 at 20:52

1 Answers1

1

It seems your file is started with Unicode BOM. Try to save your file in Utf-8 without BOM.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • 1
    So is this not supported by python? I can't find anything in the documentation that says what the allowed file encoding(s) is/are! (Lots of info about declaring your encoding via '# coding:' but none of that matters if python can't get past the first byte of the file!) – aldo Sep 30 '14 at 23:02
  • This is wrong. First off, "BOM" doesn't mean a specific sequence of bytes; it means a specific **Unicode code point**, which would be represented in the file in different ways depending on the encoding. Second, the `\xff` value suggests UTF-16 or UTF-32, not UTF-8 wherein such a byte is illegal in any position.Third, Python **accepts** files encoded in UTF-8 starting with a BOM (which in UTF-8 encoding is the bytes 0xef 0xbb 0xbf) (as long as they do not declare a *different* encoding explicitly, and as long as they are indeed encoded in UTF-8 throughout). – Karl Knechtel Mar 30 '23 at 11:24