5
votes

I'm trying to start learning Python, but I became confused from the first step. I'm getting started with Hello, World, but when I try to run the script, I get:

Syntax Error: Non-UTF-8 code starting with '\xe9' in file C:\Documents and Settings\Home\workspace\Yassine frist stared\src\firstModule.py on line 5 but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details.

4
Welcome to StackOverflow. In addition to the error, you should also post your code - it will help us understand the problem better, and you will get higher quality answers for you question! - amit
Also, please tell us which Python version you're running. (and did you follow the link you posted here?) - Tim Pietzcker

4 Answers

14
votes

add to the first line is

# -*- coding: utf-8 -*-
4
votes

Put as the first line of your program this:

# coding: utf-8

See also Correct way to define Python source code encoding

1
votes

First off, you should know what an encoding is. Read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).

Now, the problem you are having is that most people write code in ASCII. Roughly speaking, that means that they use Latin letters, numerals and basic punctuation only in the code files themselves. You appear to have used a non-ASCII character code inside your program, which is confusing Python.

There are two ways to fix this. The first is to tell Python with what encoding you would like it to read the text file. You can do that by adding a # coding declaration at the top of the tile. The second, and probably better, is to restrict yourself to ASCII code. Remember that you can always have whatever characters you like inside strings, by writing them in their encoded form as e.g. \x00 or whatever.

0
votes

When you run Python through the interpreter, you must run it in this format: python filename.py (command line args) or you will also get this error. I made the comment because you mentioned you were a beginner.