SyntaxError: Non-UTF-8 code starting with ‘\xb2‘ in file xxx.py but no encoding declared;

openCV系列文章目录

前言


# coding=gbk
import cv2
import numpy as np

def mouse_callback(event, x, y, flags, userData):
    print(event, x, y, flags, userData)


# mouse_callback(1, 100, 100, 1, '666')

cap = cv2.namedWindow('mouse', cv2.WINDOW_NORMAL)
cv2.resizeWindow('mouse', 1280, 720)

# 注册设置鼠标回调
cv2.setMouseCallback('mouse', mouse_callback, "123")
# 参数行,列,rgb,位图,功能:显示窗口和背景
img = np.zeros((720, 1280, 3), np.uint8)

while True:
    cv2.imshow('mouse', img)
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

SyntaxError: Non-UTF-8 code starting with ‘\xb2’ in file e:\openCVExercise\pythonOpenCV\mouseCallback.py on line 14, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
在这里插入图片描述
网上说:
解决方案是:

在程序最上面加上:# coding=gbk

这样程序就可以正常运行了。

解决中午乱码问题在程序最前面

#encoding=utf-8

#encoding=gb2312

#unicoding=gb2312

但这些我试过了都不行

一、问题原因

其实上面加入:# coding=gbk的方法是对的,但是我机器上不行,重新vscode也还是不行
在这里插入图片描述

二、解决办法

1.点击“运行按钮”->Run Python file

运行正常,如果直接点击运行按钮,默认是Run file,估计vscode不知道这时python代码,因为我安装了c++编译器,vscode默认用其他编译器运行,所以报错

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/aoxuestudy/article/details/129430913