Search code examples
pythonhookpyinstallerpynput

How to fix the uncompatibility with pyInstaller and pynput


I am creating an application with pyInstaller and it has a fatal error. I am using this package

from pynput.keyboard import Controller

Somebody Knows how to fix the incompatibility of pyInstaller and pynput? Maybe some hook of pyInstaller that I can use? and how to install it?

Important facts:

  • I am using python3.8 in Anaconda and spyder.
  • My script.py execute successfully.
  • I' ve created other applications (without pynput) without problems.
  • I' ve tested this with pynput 1.7 and 1.6.8.
  • I've uninstalled and re-installed PyInstall.

Here is the warn file:

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, optional), webbrowser (delayed), netrc (delayed, conditional), getpass (delayed), distutils.util (delayed, conditional, optional)
<br>
missing module named posix - imported by os (conditional, optional), 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), tarfile (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), importlib.abc (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, optional), 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, conditional), asyncio.events (delayed, conditional)
<br>
missing module named vms_lib - imported by platform (delayed, conditional, 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>

Solution

  • I have read a lot about hidden import and I tried the code

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

    It amazingly makes the .exe works normally. For other similar problems I suggest check here and here. This command is tested and works in Windows. Little changes make it useful for Linux too.

    Something I was failing is that I was omitting option "mouse" because I was not using it. These hidden imports are necessary.