Encoding error (Non-utf-8 code) while running script

Asked

Viewed 358 times

3

I made a very simple program, this way, in the notebook, and saved how Somador.py:

a = 1
b = 1

soma = a + b

print("A soma dos números é", soma)

After that I tried to run the program by cmd and gave that message:

SyntaxError: Non-UTF-8 code starting with '\xfa' in file C:\Users\Paulo\Desktop\
Somador.py on line 6, but no encoding declared; see http://python.org/dev/peps/p
ep-0263/ for details

1 answer

4

In Python 3, the parser assumes, by default, that the source code is in UTF-8. Then you probably saved the file in another encoding.

I did a test here and saved your code in a file, using the UTF-8 encoding and it runs smoothly. When I save the file in another encoding, such as ISO-8859-1, then exactly the same error occurs as yours.

So you have 2 alternatives:

  1. save the file in UTF-8 (on Notepad, just go to "Save As" and choose UTF-8)

    salvar como mostrando os encodings

    Obs: probably your file is in the "ANSI" encoding, which at the bottom is the "same" as ISO-8859-1 and/or Windows-1252 and/or Latin 1 (actually it’s a little more complicated than that, read here to learn more).

  2. if you do not want to save the file to another encoding, then in the first line of it, put the encoding that was used to save the file. In your case, it probably is:

    # -*- coding: iso-8859-1 -*-
    

    Or:

    # -*- coding: latin-1 -*-
    

    Or:

    # -*- coding: windows-1252 -*-
    

    But just do it in case the file nay is safe in UTF-8 (as already said, UTF-8 is the default, then put coding: utf-8 is unnecessary: if the file is already in UTF-8, it works without that statement, and if it is in another encoding, it will not work because the parser will attempt to read the code using UTF-8). If you chose the "ANSI" option of the Notepad, probably the 3 options above will work.


Summary:

  • Python (from version 3) assumes the source code is in UTF-8
    • if you are, wonder (don’t even need to put the statement coding: utf-8 at the beginning, as this is already the default)
    • if you are in another encoding, choose one of the options:
      • place the declaration of coding at the beginning, specifying the encoding in which the file is, or
      • save the file in UTF-8
  • I thought the encoding comment at the beginning of the file was just to inform other programmers about which encoding it. In this case, the Python interpreter reads this line and auto-configures to read the rest of the code in the specified encoding, is that it? If so, should the declaration be in the first line or in any part of the code? And is there any tool to put this statement in all the files .py of a project, so as not to have to put one on one?

  • @Jeanextreme002 According to PEP 263, It has to be in the first or second line of the file, and I don’t know the implementation details, but I imagine it "auto configure" itself. As for tools, I never used but I would guess that there must be :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.