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默认用其他编译器运行,所以报错

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值