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

PyInstaller打包后segno.QRCode缺失to_artistic属性问题求助

PyInstaller打包EXE后qrcode-artistic的to_artistic方法报错解决

问题详情

直接运行python mycode.py时,qrcode-artistic功能正常,但通过PyInstaller将代码编译为EXE文件后无法运行,PyInstaller本身未返回报错。运行环境为Python 3.11.9、segno 1.5.2、qrcode-artistic 2.1.0,已确认qrcode-artistic安装正常。

报错信息

File "ORIGINAL_code.py", line 349, in <module>
      File "ORIGINAL_code.py", line 323, in main
      File "segno\__init__.py", line 970, in __getattr__
    AttributeError: <class 'segno.QRCode'> object has no attribute to_artistic
[7552] Failed to execute script 'ORIGINAL_code' due to unhandled exception!

相关代码片段

qr = helpers.make_wifi(ssid, guestPsk, security)
qr.to_artistic(background='green.png', target='QR-CODE.png', dark='#012A2D', data_dark='#003F2D', data_light='white', scale=8)

解决方法

  • 问题根源:qrcode-artistic是通过猴子补丁给segno.QRCode动态添加to_artistic方法的,PyInstaller打包时默认没识别到这个隐式依赖,导致打包后的EXE中该方法缺失。

  • 具体方案:

    1. 显式导入依赖:在代码开头添加一行导入,确保PyInstaller能捕获到qrcode-artistic模块:
      import qrcode_artistic  # 注意这里用下划线,对应包的实际导入名
      
    2. 打包时强制包含模块:使用PyInstaller的--hidden-import参数指定要包含的模块,打包命令改为:
      pyinstaller --hidden-import qrcode_artistic mycode.py
      
    3. 处理资源文件:确保背景图green.png被正确打包,可通过PyInstaller的--add-data参数添加资源,同时在代码中适配路径:
      # Windows系统打包命令
      pyinstaller --hidden-import qrcode_artistic --add-data "green.png;." mycode.py
      # Linux/macOS系统打包命令
      pyinstaller --hidden-import qrcode_artistic --add-data "green.png:." mycode.py
      
      代码中修改路径获取逻辑:
      import sys
      import os
      
      def get_resource_path(relative_path):
          try:
              # 打包后的临时目录
              base_path = sys._MEIPASS
          except Exception:
              # 未打包时的当前目录
              base_path = os.path.abspath(".")
          return os.path.join(base_path, relative_path)
      
      # 使用函数获取背景图路径
      qr.to_artistic(background=get_resource_path('green.png'), target='QR-CODE.png', dark='#012A2D', data_dark='#003F2D', data_light='white', scale=8)
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 15:35:55