Send by LINE
B! Bookmarks in Hate-bu
Bookmarks in Pocket
RSS feeds

Python, How to fix "SyntaxError: Non-ASCII character"

python image

When I run python in a Linux environment, I get the error "SyntaxError: Non-ASCII character '\xe3' in file…" error.

This can be solved by simply adding a line to the beginning of the .py file.

This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.

I uploaded a python file created in the windows environment to the Linux environment and ran it.

The Python version is 2.7.5. This is the pre-installed version of Python in CentOs, and the CentOs version is "CentOS Linux release 7.3.1611 (Core)".

However, I get the following error.

SyntaxError: Non-ASCII character '\xe3' in file /●●●/ on line 15, but no encoding declared; see for details


An error is thrown if the comments or standard output string contains a non-English language such as Japanese or a string of symbols such as emoticons.

That is, if the .py file contains a non-English language.

It happens even if you create it in UTF-8, because Python is designed to recognize ASCII code by default.

I said "non-English" earlier, but it's more accurately "non-ASCII code".

ASCII code is

American Standard Code for Information Interchange

This is 32 2-byte characters. (Alphabetic and symbolic, DEL and other codes)

For more information, please refer to the list of ASCII codes here.

Add one more line and a quick fix

Add the following to the first line of the .py file

# coding:utf-8

When your program consists of multiple files, write them all in a .py file.

When you create a bash file, you can use the


write. If you make it a habit to feel like this is something you must put on, you won't waste your time on unnecessary errors.

When you use non-ASCII code in python, you need to specify the character set.

Author's Comment

I tried to find somewhere to specify the character code in the preferences like php and ruby, but it doesn't seem to be available in python.

Nowadays, it is commonplace to write and execute source code on an integrated development environment (IDE) such as eclipse.

When you run in such an environment, there is a high probability that the IDE is automatically modifying and executing the code and no errors are generated.

(You often specify the character code in the configuration screen.)

Actually, in my case, the error occurred even though it worked fine on eclipse.

When the automation is advanced and convenient, these rudimentary things can take up a lot of time.

It's good that it makes the work easier, but I'm reminded that sometimes it can be worse.

Leave a Reply