You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将URL安全检测QR扫描器Python程序打包为可执行文件?

解决PyInstaller打包QR扫描器后闪退问题

问题概述

我开发了一款检测URL安全性的QR扫描器Python程序,需打包为Windows可执行文件供其他电脑使用。用PyInstaller打包后,程序打开瞬间就关闭,无法执行任何功能,代码如下:

import cv2
from pyzbar.pyzbar import decode
import time
import requests
from bs4 import BeautifulSoup

cam = cv2.VideoCapture(0)
cam.set(5, 640)
cam.set(6, 480)

camera = True

while camera == True:
    sucess, frame = cam.read()

    for i in decode(frame):
        page = i.data.decode("utf-8")
        print(page)

        if "." in page:
            linkcheckeo = ("https://safeweb.norton.com/report/show?url="+page)
            respuesta = requests.get(linkcheckeo)
            respuesta = respuesta.content
            soup = BeautifulSoup(respuesta, "html.parser")
            resultados = soup.find(id="bodyContent")
            titulo = resultados.find_all("b")
            titulo = titulo[0].text
            if titulo == "SAFE":
                print("SEGURO")
            elif titulo == "CAUTION":
                print("PRECAUCION")
            elif titulo == "WARNING":
                print("QR PELIGROSO")
            elif titulo == "UNTESTED":
                print("ORIGEN DESCONOCIDO")
            else:
                print(titulo)
        else:
            print("No detectamos un URL")
        time.sleep(6)

    cv2.imshow("Qr_Code_Scanner", frame)
    cv2.waitKey(3)

可能的原因及解决方法

1. 控制台窗口关闭太快,无法查看错误

  • 打包时添加--console参数强制显示控制台,保留错误输出:
    pyinstaller --console your_script_name.py
    
  • 或者手动打开命令提示符,定位到exe文件路径后直接运行,控制台会保留报错信息。

2. 依赖库未被正确打包

PyInstaller可能遗漏了cv2、pyzbar等库的依赖文件,需手动指定:

  • 打包时用--hidden-import明确导入缺失库:
    pyinstaller --console --hidden-import cv2 --hidden-import pyzbar --hidden-import requests --hidden-import bs4 your_script_name.py
    
  • 对于pyzbar,需将Python安装目录下Lib\site-packages\pyzbar中的libzbar-0.dll复制到exe所在目录。

3. 代码拼写错误导致崩溃

代码中sucess是拼写错误,应为success,这个错误会触发异常直接终止程序:

# 原代码
sucess, frame = cam.read()
# 修改为
success, frame = cam.read()

4. 摄像头访问权限不足

Windows下打包后的exe可能没有摄像头访问权限:

  • 打开Windows设置→隐私和安全性→相机,允许该应用访问相机;
  • 右键exe选择「以管理员身份运行」尝试。

5. 循环无退出逻辑,窗口关闭触发异常

原代码没有退出循环的条件,关闭窗口后程序可能因异常崩溃,添加退出逻辑:

while camera == True:
    success, frame = cam.read()
    # ... 原有代码 ...
    cv2.imshow("Qr_Code_Scanner", frame)
    # 按下ESC键退出循环
    if cv2.waitKey(3) == 27:
        camera = False

# 释放摄像头资源
cam.release()
cv2.destroyAllWindows()

6. 网络请求无异常处理

依赖Norton接口的网络请求失败会直接崩溃程序,添加异常捕获:

if "." in page:
    linkcheckeo = ("https://safeweb.norton.com/report/show?url="+page)
    try:
        respuesta = requests.get(linkcheckeo)
        respuesta.raise_for_status()  # 捕获HTTP错误
    except requests.exceptions.RequestException as e:
        print(f"网络请求错误: {e}")
        continue
    # ... 原有解析代码 ...

验证步骤

  1. 先修复代码拼写错误,添加异常处理和退出逻辑;
  2. 用--console参数重新打包;
  3. 在命令提示符中运行exe,查看错误输出;
  4. 根据错误信息补充依赖或调整权限。

内容的提问来源于stack exchange,提问作者Eduardox33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:58:19