5

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.

jotik
  • 16,015
  • 11
  • 53
  • 110
user1929872
  • 57
  • 1
  • 1
  • 2
  • 11
    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 Dec 26 '12 at 13:09
  • Also, please tell us which Python version you're running. (and did you follow the link you posted here?) – Tim Pietzcker Dec 26 '12 at 13:10

4 Answers4

14

add to the first line is

# -*- coding: utf-8 -*-
user1928850
  • 141
  • 4
  • With that as the first line I get `SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xb0 in position 0: invalid start byte` – Hack-R Feb 16 '16 at 20:09
4

Put as the first line of your program this:

# coding: utf-8

See also Correct way to define Python source code encoding

Community
  • 1
  • 1
warvariuc
  • 53,721
  • 35
  • 166
  • 222
2

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.

Katriel
  • 114,760
  • 19
  • 131
  • 163
  • If the intention is to write one's first python script (perhaps one's first program), that document may be putting the cart before the horses. – cmh Dec 26 '12 at 13:27
  • @cmh well, perhaps, but it doesn't require any programming knowledge to understand what an encoding is! And it gives a good foundation for writing modern encoding-aware programs. – Katriel Dec 26 '12 at 13:28
0

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.

Kai Tribble
  • 108
  • 9