1

So I'm running a python GUI script, but it gives me the following error:

SyntaxError: Non-UTF-8 code starting with '\x92' in file D:\AIAssistant\build\gui.py on line 92, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

And I'm using this script:


window = Tk()

window.geometry("1440x1024")
window.configure(bg = "#FFFFFF")


canvas = Canvas(
    window,
    bg = "#FFFFFF",
    height = 1024,
    width = 1440,
    bd = 0,
    highlightthickness = 0,
    relief = "ridge"
)

canvas.place(x = 0, y = 0)
canvas.create_rectangle(
    28.0,
    22.0,
    534.0,
    140.0,
    fill="#06FF96",
    outline="")

canvas.create_rectangle(
    28.0,
    367.0,
    534.0,
    485.0,
    fill="#06FF96",
    outline="")

canvas.create_rectangle(
    28.0,
    702.0,
    534.0,
    820.0,
    fill="#06FF96",
    outline="")

canvas.create_rectangle(
    951.0,
    165.0,
    1412.0,
    283.0,
    fill="#00C5F1",
    outline="")

canvas.create_rectangle(
    951.0,
    498.0,
    1412.0,
    616.0,
    fill="#00C5F1",
    outline="")

canvas.create_text(
    55.0,
    52.0,
    anchor="nw",
    text="Hi there! How can I help?",
    fill="#000000",
    font=("RobotoCondensed Light", 40 * -1)
)

canvas.create_text(
    53.0,
    400.0,
    anchor="nw",
    text="I’m 3y/o on this March. ",
    fill="#000000",
    font=("RobotoCondensed Light", 40 * -1)
)

canvas.create_text(
    55.0,
    735.0,
    anchor="nw",
    text="I’m 3y/o on this March. ",
    fill="#000000",
    font=("RobotoCondensed Light", 40 * -1)
)

canvas.create_text(
    971.0,
    199.0,
    anchor="nw",
    text="Open Google",
    fill="#000000",
    font=("RobotoCondensed Light", 40 * -1)
)

canvas.create_text(
    979.0,
    531.0,
    anchor="nw",
    text="Open Google",
    fill="#000000",
    font=("RobotoCondensed Light", 40 * -1)
)

entry_image_1 = PhotoImage(
    file=relative_to_assets("entry_1.png"))
entry_bg_1 = canvas.create_image(
    607.0,
    957.5,
    image=entry_image_1
)
entry_1 = Entry(
    bd=0,
    bg="#FFFFFF",
    highlightthickness=0
)
entry_1.place(
    x=55.0,
    y=920.0,
    width=1104.0,
    height=73.0
)

button_image_1 = PhotoImage(
    file=relative_to_assets("button_1.png"))
button_1 = Button(
    image=button_image_1,
    borderwidth=0,
    highlightthickness=0,
    command=lambda: print("button_1 clicked"),
    relief="flat"
)
button_1.place(
    x=1176.0,
    y=919.0,
    width=118.0,
    height=75.0
)
window.resizable(False, False)
window.mainloop()

I searched for this question too but didn't find the right solution. And the most important thing is I'm using Python 3.10.1. The file was generated by TKdesigner and my os system is Windows 10.

Thanks in advance!

JKR
  • 69
  • 1
  • 1
  • 10
  • did you try to do what is suggested in the link? declaring the encoding? also can't reproduce on Python 3.8 (tho seemingly it has nothing to do with the version but more with encoding, especially since the file was automatically generated) – Matiiss Dec 25 '21 at 12:00
  • You use the character `’` (U+2019, *Right Single Quotation Mark*), and `\x92` is it's code in Windows code pages `cp1252`, `cp1250` (and more others). Save your script using `utf-8` encoding (and use `# -*- coding: utf-8 -*-` if you are on deprecated Python version 2). – JosefZ Dec 25 '21 at 13:30
  • Does this answer your question? [SyntaxError: Non-UTF-8 code starting with '\x91'](https://stackoverflow.com/questions/23092176/syntaxerror-non-utf-8-code-starting-with-x91) – Ulrich Eckhardt Dec 26 '21 at 09:26
  • 1
    Two things: The whole length of your script is irrelevant, it should be a [mcve], where one or two lines probably suffice. Further, just search online for the error message. If there are similar questions, read and understand them first! – Ulrich Eckhardt Dec 26 '21 at 09:27

3 Answers3

4

Use the below as it is and insert it on top of the code.

# coding: utf8

NOTE: It must be at the top.

  • 1
    **This is wrong**. The specific error OP got demonstrates that the code is being run in 3.x, which means that **UTF-8 is the default anyway and does not need to be specified**. The problem occurs because OP uses an encoding that is **not** UTF-8. – Karl Knechtel Mar 16 '23 at 08:24
2

Python 3 expects source files to be encoded in UTF-8. If a different encoding is used, it must be declared as the link in the error message describes.

Make sure to save the source file in UTF-8, or declare the encoding. On Windows 10 (Western European or US localized version), the file is likely Windows-1252-encoded if not UTF-8. Adding a # coding: cp1252 comment at the top of the file will declare the encoding in this case, but it's better to just use UTF-8 if you can figure out how to change the encoding in your editor.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
1

Adding this as tee first line worked for me : # coding: cp1252

3Lok
  • 109
  • 1
  • 3