SyntaxError: Non-ASCII character '\xe3' in file G:\test.py on line 2, but no encoding declared

13,634

Solution 1

Your comment doesn't fit the requirements of the PEP because you left a space between the word encoding and the colon:

# -*- encoding : utf-8 -*-
#             ^

The PEP specifies the exact regular expression used to match the declaration:

coding[:=]\s*([-\w.]+)

So Python looks for the literal text coding followed by either a colon : or an equals sign =, followed by zero or more whitespace characters, then followed by the codec.

All you need to do is remove that extra space you have:

# -*- encoding: utf-8 -*-

The comment applies only when Python has to read the source code from disk; in an interactive interpreter you enter your code with a console or terminal that also declares an encoding, through the normal localisation support of the platform. IPython and PyCharm fall under this category; you keyboard input is correctly decoded using that information and the declaration is not needed.

Solution 2

Just replace

# -*- encoding : utf-8 -*-

with

# encoding: utf-8
Share:
13,634
Aaron
Author by

Aaron

I'm a dentist in Taiwan. Learning Python as my first programming language now!

Updated on June 15, 2022

Comments

  • Aaron
    Aaron about 2 years

    I have a simple script.

    # -*- encoding : utf-8 -*-
    episode_title = "ザ・ロック(日本語吹替版)".decode('utf-8')
    print episode_title.encode('utf-8')
    

    But it doesn't work at my cmd console (Win8 PC, encoding is big5).

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

    Although I have change the code page to utf-8

    chcp 65001 
    

    It still doesn't work.

    But when I run it in my IPython notebook, it works! (Works also in PyCharm and the IPython Qt console)

    ザ・ロック(日本語吹替版)
    

    But when I run it in Ubuntu:

    aaron@ubuntu:~$ python test.py
      File "test.py", line 2
    SyntaxError: Non-ASCII character '\xe3' in file test.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    

    It surprised me that it cannot run in Ubuntu.

    Would someone please explain this mystery?

    Why it run in IPython Notebook or PyCharm? but not in CMD or Ubuntu Terminal?