Python Forum
How to solve this problem Python configuration? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to solve this problem Python configuration? (/thread-41817.html)



How to solve this problem Python configuration? - magomes - Mar-22-2024

This error.
SyntaxError: Non-UTF-8 code starting with '\xe3' in file D:\Projetos\M74Chat\M74Chat\config.py on line 4, but no encoding decared;
see https://peps.python.org/pep-0263/ for details

My code.
import psycopg2
connection made successfully, generates this error
# and cursor.execute('SELECT * FROM contact')

I don't believe the only solution to this problem is to put in all .py files on the first line: "# coding: utf-8", serious?


RE: How to solve this problem Python configuration? - deanhystad - Mar-22-2024

Please post code and complete error message, including the traceback.

\x is the start of an escape sequence for a unicode character. This should be followed by two digits that define the unicode character. \xe3 is a lower case "a" with tilde. I have no problem using in a program like this:
print("\\xe3 is a lower-case a with a tilde \xe3")
Notice that I do not specify the encoding of the source file. I am running windows 10 and python 3.11. What are you running?


RE: How to solve this problem Python configuration? - magomes - Mar-22-2024

Thanks, deanhystad.
I don't know exactly what happened after using
# coding = utf-8
I removed it from the code and it's working.

This my code:
import psycopg2

try:
  # Conexão ao banco de dados
  con = psycopg2.connect(dbname="*", user="*", password="*", host="*", port="*")
  # Criando um cursor para executar comandos SQL
  cursor = con.cursor()
  
connected to DB

  # Executando uma consulta SQL
  cursor.execute("SELECT * FROM contact")
  # Obtendo os resultados da consulta
  resultado = cursor.fetchall()
  # Imprimindo os resultados
  for linha in resultado:
    print(linha)
  # Fechando o cursor e a conexão
  cursor.close()
  con.close()
except Exception as e:
  print("Erro ao conectar ao banco de dados: ", e)
The message in oder post.


RE: How to solve this problem Python configuration? - deanhystad - Mar-22-2024

How do you edit your files? I've noticed it is easy to confuse windows into thinking a file is latin1
Quote:The message in oder post.
That was the entire error message? Nothing else?


RE: How to solve this problem Python configuration? - Gribouillis - Mar-22-2024

Aren't you using Python 2.7 instead of Python 3 ?

Also if you have a file encoded in some other encoding and you add # coding: utf8 at the top of the file, this may confuse the Python compiler.


RE: How to solve this problem Python configuration? - magomes - Mar-22-2024

(Mar-22-2024, 06:52 PM)deanhystad Wrote: How do you edit your files? I've noticed it is easy to confuse windows into thinking a file is latin1
Quote:The message in oder post.
That was the entire error message? Nothing else?

check image in Attachment.