如何解决与 pyInstaller 和 pynput 不兼容的问题

问题描述

我正在使用 pyInstaller 创建一个应用程序,但它有一个致命错误。我正在使用这个包

从 pynput.keyboard 导入控制器

有人知道如何解决pyInstaller和pynput不兼容的问题吗? 也许我可以使用一些 pyInstaller 钩子?以及如何安装?

重要事实:

  • 我在 Anaconda 和 spyder 中使用 python3.8。
  • 我的 script.py 执行成功。
  • 我已经创建了其他应用程序(没有 pynput)而没有问题。
  • 我已经使用 pynput 1.7 和 1.6.8 对此进行了测试。
  • 我已经卸载并重新安装了 PyInstall。

这是警告文件

missing module named _posixsubprocess - imported by subprocess (optional),multiprocessing.util (delayed)
<br>
missing module named pwd - imported by posixpath (delayed,conditional),shutil (optional),tarfile (optional),pathlib (delayed,conditional,optional),http.server (delayed,webbrowser (delayed),netrc (delayed,getpass (delayed),distutils.util (delayed,optional)
<br>
missing module named posix - imported by os (conditional,shutil (conditional)
<br>
missing module named resource - imported by posix (top-level),test.support (optional)
<br>
missing module named 'org.python' - imported by copy (optional),xml.sax (delayed,conditional)
<br>
missing module named _posixshmem - imported by multiprocessing.resource_tracker (conditional),multiprocessing.shared_memory (conditional)
<br>
missing module named multiprocessing.set_start_method - imported by multiprocessing (top-level),multiprocessing.spawn (top-level)
<br>
missing module named multiprocessing.get_start_method - imported by multiprocessing (top-level),multiprocessing.spawn (top-level)
<br>
missing module named grp - imported by shutil (optional),pathlib (delayed)
<br>
excluded module named _frozen_importlib - imported by importlib (optional),importlib.abc (optional),zipimport (top-level)
<br>
missing module named _frozen_importlib_external - imported by importlib._bootstrap (delayed),importlib (optional),zipimport (top-level)
<br>
missing module named multiprocessing.get_context - imported by multiprocessing (top-level),multiprocessing.pool (top-level),multiprocessing.managers (top-level),multiprocessing.sharedctypes (top-level)
<br>
missing module named multiprocessing.TimeoutError - imported by multiprocessing (top-level),multiprocessing.pool (top-level)
<br>
missing module named _scproxy - imported by urllib.request (conditional)
<br>
missing module named termios - imported by tty (top-level),getpass (optional)
<br>
missing module named 'java.lang' - imported by platform (delayed,xml.sax._exceptions (conditional)
<br>
missing module named multiprocessing.BufferTooShort - imported by multiprocessing (top-level),multiprocessing.connection (top-level)
<br>
missing module named multiprocessing.AuthenticationError - imported by multiprocessing (top-level),multiprocessing.connection (top-level)
<br>
missing module named asyncio.DefaultEventLoopPolicy - imported by asyncio (delayed,asyncio.events (delayed,conditional)
<br>
missing module named vms_lib - imported by platform (delayed,optional)
<br>
missing module named java - imported by platform (delayed)
<br>
missing module named _winreg - imported by platform (delayed,optional)
<br>
missing module named console - imported by pyreadline.console.ansi (conditional)
<br>
missing module named startup - imported by pyreadline.keysyms.common (conditional),pyreadline.keysyms.keysyms (conditional)
<br>
missing module named sets - imported by pyreadline.keysyms.common (optional)
<br>
missing module named System - imported by pyreadline.clipboard.ironpython_clipboard (top-level),pyreadline.keysyms.ironpython_keysyms (top-level),pyreadline.console.ironpython_console (top-level),pyreadline.rlmain (conditional)
<br>
missing module named StringIO - imported by pyreadline.py3k_compat (conditional),six (conditional)
<br>
missing module named IronPythonConsole - imported by pyreadline.console.ironpython_console (top-level)
<br>
missing module named clr - imported by pyreadline.clipboard.ironpython_clipboard (top-level),pyreadline.console.ironpython_console (top-level)
<br>
missing module named org - imported by pickle (optional)
<br>
runtime module named six.moves - imported by pynput._util (top-level)
<br>

解决方法

我已经阅读了很多关于隐藏导入的内容,并尝试了代码

pyinstaller --hidden-import=pynput.keyboard._win32 --hidden-import=pynput.mouse._win32 script.py

它令人惊讶地使 .exe 正常工作。对于其他类似问题,我建议检查 herehere。此命令已在 Windows 中测试并有效。小改动也让它对 Linux 有用。

我失败的地方是我省略了选项“鼠标”,因为我没有使用它。 这些隐藏的导入是必要的