4

This is the unicode that I have defined at the top of my program

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

And yet I still get this error

SyntaxError: Non-ASCII character '\xfe' in file C:/Users/aaron/Desktop/Python/Bicycle_Diagnosis_System/Main.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I have looked at the website it wprovides and trawled other websites and still can't find the answer. Any ideas (im using pycharm community edition as my IDE if that affects it)

Any help is much appreciated!

3
  • Are you sure your file is actually UTF-8? Can you show a hex dump of a few characters around the problematic one? Commented Mar 1, 2018 at 10:44
  • could you at least provide a snippet of the code? Commented Mar 1, 2018 at 20:34
  • A byte \xfe in the first line could be the start of a byte order mark. This would mean that your file was encoded with UTF-16 (on Windows often called "Unicode") on a big-endian machine. Not only is your encoding declaration wrong in that case: UTF-16 isn't allowed as source code encoding altogether, because it isn't backwards-compatible to ASCII. Commented Mar 1, 2018 at 21:24

2 Answers 2

4

Trying add #coding=utf-8 on line 1 and re-run

Sign up to request clarification or add additional context in comments.

Comments

0

You file is saved as UTF-16 with BOM (big-endian). I saved your sample code in Notepad++ with that encoding and reproduced the error:

  File "test.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file x.py on line 1, but no encoding declared; see http://python.org/dev/peps
/pep-0263/ for details

Make sure your file is saved in the encoding declared. You have to use an encoding compatible with ASCII for the hash bang and encoding lines to be read properly. UTF-16 is not compatible, hence the error message when it read the non-ASCII bytes of the byte order mark (BOM) character.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.