Enable UTF-8 Unicode In Python Source File

December 19, 2017

If you add unicode in your source file, Python will prompt Non-ASCII character error.

str = u'Hello, 我爱你'

SyntaxError: Non-ASCII character '\xe6' in file __init__.py on line 64, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

To enable UTF-8 encoding in python, add the following at the top of your source file.

# -*- coding: utf-8 -*-
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.