0

I converted a python file to exe file, but when I am opening it it's not working. Here is the error traceback:

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Roaming\python\Python39\site-packages\test\test.py", line 1, in <module>
  File "C:\Users\Admin\AppData\Roaming\Python\Python39\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "pynput\__init__.py", line 40, in <module>
.....
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[16684] failed to execute script test

I don't know what it means. How can I stop this and make my file work?

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • There is an "ImportError", looks like you (or the modules you are importing) are trying to import something that the system can't find. Check out this link: https://stackoverflow.com/questions/63681770/getting-error-when-using-pynput-with-pyinstaller – User2332 Jan 05 '21 at 21:12
  • Is there more to that traceback? What did you use to create the .exe? Can you post a [mre] of the code that causes this issue? – Random Davis Jan 05 '21 at 21:12
  • Are you running the exe on the same computer where you ran the original python script? – John Gordon Jan 05 '21 at 21:20

1 Answers1

1

It appears that the import of pynput is in bytecode. That makes it impossible for pyinstaller to find it: it is a hidden import. The documentation has this to say:

Hidden imports can occur when the code is using __import__, imp.find_module() 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 --debug=imports flag ... and run it.

Once you know what modules are needed, you add the needed modules to the bundle using the --hidden-import= command option.

BoarGules
  • 16,440
  • 2
  • 27
  • 44