How to Resolve "SyntaxError: Non-ASCII character '\xe2' but no encoding declared" in Python
When running a Python script, you might encounter the SyntaxError: Non-ASCII character '\xe2' ... but no encoding declared. This error occurs almost exclusively in Python 2 and is a direct result of the interpreter finding a character that is not part of the default ASCII encoding. The \xe2 is the first byte of a multi-byte character (like a smart quote, em-dash, or currency symbol) encoded in UTF-8, which Python 2 does not handle by default.
This guide will explain why this error happens and provide two solutions: the immediate fix for legacy code and the highly recommended long-term solution of upgrading to Python 3.
Understanding the Error: Python 2's Default ASCII Encoding
- ASCII: A simple, 7-bit character encoding that includes 128 characters, covering basic English letters, numbers, and symbols. Python 2 defaults to interpreting source code files as ASCII.
- UTF-8: A variable-width character encoding that can represent every character in the Unicode standard. It is the dominant encoding for the web and the default for Python 3 source code.
Characters like the en-dash (–), smart quotes (’), or the Euro sign (€) are not in the ASCII set. When the Python 2 interpreter encounters the bytes for one of these characters in a file, it doesn't know how to process them and raises a SyntaxError.
Reproducing the SyntaxError
This error is often caused by copying and pasting code from a website, which may replace standard characters like - or ' with their typographic (non-ASCII) equivalents.
Example of code causing the error (in Python 2):
# This script is saved as main.py and run with a Python 2 interpreter.
# The dash between "One" and "liner" is an en-dash (–), not a hyphen (-).
my_str = "One–liner string"
print my_str
Output:
File "main.py", line 1
SyntaxError: Non-ASCII character '\xe2' in file script.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Solution 1: Declare the Source Code Encoding (PEP 263)
The error message itself points to the solution outlined in PEP 263. You can tell the Python 2 interpreter to read the file using a specific encoding (like UTF-8) by adding a special "magic comment" at the very top of your file.
Solution:
# -*- coding: utf-8 -*-
# This "magic comment" tells Python 2 to interpret the file as UTF-8.
my_str = "One–liner string"
print my_str
Output (when run with Python 2):
One–liner string
This comment must be on the first or second line of your Python script.
Solution 2: Upgrade to Python 3 (Highly Recommended)
The best long-term solution is to migrate your code to Python 3. Python 3 defaults to UTF-8 for source code, so this problem is completely eliminated.
Python 2 has been end-of-life (EOL) since January 1, 2020. It no longer receives security updates and is a significant liability. All new development should be in Python 3, and legacy projects should be migrated.
Solution: the original code that failed in Python 2 works perfectly in Python 3 without any modifications.
# This script is saved as main.py and run with a Python 3 interpreter.
my_str = "One–liner string"
print(my_str) # Note: print is a function in Python 3
To run this from the terminal:
python3 main.py
Output:
One–liner string
How to Find the Problematic Character
Sometimes the non-ASCII character can be visually indistinguishable from its ASCII counterpart (e.g., a smart quote ’ vs. a straight quote '). You can use a simple script to scan your file and identify which lines contain non-ASCII bytes.
The Helper Script:
# Replace 'main.py' with the name of the file causing the error.
filename = 'main.py'
try:
with open(filename, 'rb') as f: # Open in binary mode
for i, line in enumerate(f, 1):
try:
line.decode('ascii')
except UnicodeDecodeError:
print(f"Non-ASCII character found on line {i}:")
print(repr(line))
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
This script will pinpoint the exact lines and show you a representation of the bytes, making it easy to find and fix the problematic characters.
Conclusion
| Your Situation | Recommended Solution |
|---|---|
| You are maintaining a legacy Python 2 project and cannot upgrade. | Add the encoding comment # -*- coding: utf-8 -*- to the top of your files. |
| You are starting a new project or have the ability to migrate. | Upgrade your environment and code to Python 3. This is the correct, modern, and secure solution. |
The SyntaxError: Non-ASCII character is a relic of Python 2's outdated default encoding. While the "magic comment" provides an immediate fix, the true solution is to move to Python 3, where UTF-8 is the standard and such errors no longer occur.