如何通过 pyinstaller 安装带有 tkcalendar 模块的 python 应用程序?

How to install python application with tkcalendar module by pyinstaller?

我正在尝试使用我正在使用 tkcalendar 的 pyinstaller 在 Windows 上安装 python 应用程序。应用程序正在运行,但 tkcalendar.Calendar 未运行。

当我 运行 在未安装的情况下安装应用程序时,一切正常,但如果我这样做,日历小部件不会出现。 我认为 pyinstaller 看到了这个模块,但他对 tkcalendar 使用的模块有问题。 我尝试 运行 pyinstaller 与 --path=/.../python/Lib/site-packages 但这没有用。将模块文件复制到应用程序目录也没有帮助。

如果有人发现同样的问题。 在 tkcalendar 1.5.0 中 calendar.py.

中的导入有问题

找到 tkcalendar 文件夹(可能是 /.../python/Lib/site-packages/tkcalendar)并在 calendar.py 下添加一个缺少模块的附加导入:

import calendar
from babel.dates import format_date, parse_date, get_day_names, get_month_names
from babel.numbers import *  # Additional Import```

问题并非来自 tkcalendar,而是 PyInstaller 未检测到二级导入这一事实。在 HowTos 部分的 tkcalendar 文档中解释了解决此问题的方法:

When bundling an application with PyInstaller, there is an issue with the detection of the babel dependency of tkcalendar. This can be fixed by using the --hidden-import option:

$ pyinstaller --hidden-import babel.numbers myscript.py

or by editing the .spec file:

hiddenimports=["babel.numbers"]

将以下代码添加到您的 python 脚本中,同时与 pyinstaller

捆绑在一起
import babel.numbers