Enable UTF-8 Unicode In Python Source File

Dec 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 -*-

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.