ImportError: No module named win32timezone when i make a singleone exe from a python script with pyInstaller

ImportError: No module named win32timezone when i make a singleone exe from a python script with pyInstaller

我有一个脚本,当它是一个 py 文件时,它可以工作,但是当我使用 pyInstaller 从该脚本制作一个单独的 exe 文件时,我会得到一个错误:ImportError: No module named win32timezone.
我在脚本中导入:

import win32serviceutil
import win32service
import win32event

和其他一些模块,如 subprocess, os, time,但我认为 3 个是问题所在。
有人知道怎么了吗?谢谢!

我认为您需要遵循 Pyinstaller 手册的这一部分:

https://pythonhosted.org/PyInstaller/#id67

Listing Hidden Imports

If Analysis thinks it has found all the imports, but the app fails with an import error, the problem is a hidden import; that is, an import that is not visible to the analysis phase.

Hidden imports can occur when the code is using import or perhaps exec or eval. Hidden imports can also occur when an extension module uses the Python/C API to do an import. When this occurs, Analysis can detect nothing. There will be no warnings, only an ImportError at run-time.

To find these hidden imports, build the app with the -v flag (Getting Python's Verbose Imports above) and run it.

Once you know what modules are needed, you add the needed modules to the bundle using the --hidden-import= command option, or by editing the spec file, or with a hook file (see Understanding PyInstaller Hooks below).

-v 标志不再有效(现在显示版本)。

首先,找出缺少的模块。您可以通过 PowerShell/cmd 执行 exe 来完成此操作。例如,如果您的文件是 "project.exe",请在其目录中打开 PowerShell window 并使用命令:.\project.exe.

使用它来构建 exe: pyinstaller --hiddenimport win32timezone -F a.py

  • win32timezone 是缺少的模块。
  • 使用 -F 或 --onefile 创建一个独立的、可再分发的 exe。
  • 如果缺少多个模块,可以多次使用 --hiddenimport。

参考:https://pythonhosted.org/PyInstaller/usage.html