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

py2exe打包可执行文件时出现packaging缺失ImportError问题咨询

py2exe打包win10toast程序报packaging缺失错误

问题现象

开发了一款可连接MQTT Broker、通过win10toast实现Windows 10系统通知推送的软件,使用py2exe打包为exe后,运行程序抛出如下ImportError:

Traceback (most recent call last):
  File "suscribe.py", line 7, in <module>
  File "zipextimporter.pyc", line 168, in exec_module
  File "win10toast\__init__.pyc", line 15, in <module>
  File "zipextimporter.pyc", line 168, in exec_module
  File "pkg_resources\__init__.pyc", line 75, in <module>
  File "pkg_resources\extern\__init__.pyc", line 52, in create_module
  File "pkg_resources\extern\__init__.pyc", line 44, in load_module
ImportError: The 'packaging' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.

当前配置

setup.py配置

from distutils.core import setup
import py2exe
from playsound import playsound 
import random
import time

from paho.mqtt import client as mqtt_client
from win10toast import ToastNotifier
packages = ['pkg_resources','win10toast','zipextimporter']
setup(
    name="Nombre ejecutable",
    version="1.0",
    description="Breve descripcion",
    author="autor",
    author_email="email del autor",
    url="url del proyecto",
    license="tipo de licencia",
    scripts=["suscribe.py"],
    console=["suscribe.py"],
    options={"py2exe": {"bundle_files": 1},
            'build_exe': {'packages':packages}
    },
    zipfile=None,
)

主程序导入语句

from playsound import playsound 
import random
import time

from paho.mqtt import client as mqtt_client
from win10toast import ToastNotifier

解决步骤

  • 先确认本地Python环境已安装packaging依赖,执行安装命令:
    pip install packaging
  • 修改setup.py中的包收集配置,把缺失的packaging以及其他用到的第三方依赖加入packages列表,同时补充includes配置强制打包packaging的子模块(py2exe无法自动递归收集这些子模块),修改后的options配置如下:
packages = ['pkg_resources','win10toast','zipextimporter','packaging','paho.mqtt','playsound']
setup(
    # 其他原有配置保持不变
    options={
        "py2exe": {"bundle_files": 1},
        'build_exe': {
            'packages': packages,
            'includes': ['packaging.version', 'packaging.specifiers', 'packaging.requirements', 'packaging.markers']
        }
    },
    zipfile=None,
)
  • 删除之前打包生成的build、dist缓存目录,重新执行打包命令即可:
    python setup.py py2exe

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 04:54:37