8

A friend of mine asked me to write him a program, and I used pynput to handle some of the inputs and other features. When I convert the program an executable with pyinstaller, launcing the executable gives me this error: File "site-packages\pynput\keyboard\__init__.py", line 31, in <module> File "site-packages\pynput\_util\__init__.py", line 82, in backend ImportError [11492] Failed to execute script friend_project

I have tried using the pyinstaller command pyinstaller --onefile friend_project.py, and also using auto-py-to-exe to run it.

Using pyinstaller with other modules like pygame or pyopengl gives me no error, but this one module does. Running the script by it self with the python inturpeter works fine, but I would perfer to have it be an exe so I can give it to him with out him needing python to run it.

5
  • Have you tried hidden imports when building the exe?
    – SajanGohil
    Commented Sep 1, 2020 at 5:44
  • @SajanGohil Yes, but I will try it again to see if it works.
    – user13140483
    Commented Sep 1, 2020 at 6:29
  • I could use pynput with pyinstaller to pack my py file in the past. Commented Sep 1, 2020 at 16:19
  • @jizhihaoSAMA was there a specific way you typed the command? If not I might have a problem with the version of pynput I am using...
    – user13140483
    Commented Sep 1, 2020 at 16:29
  • Just pyinstaller -F x.py.The same as yours. Commented Sep 1, 2020 at 16:29

2 Answers 2

23

Please fall back to 1.6.8 version of pynput. pip install pynput==1.6.8

4
  • 1
    Thank you. Saved my life. Just one question how would I find out something like this. I assumed something was wrong with version of pynput but how would I know which version to revert to is there a list supported versions?
    – Borut Flis
    Commented Feb 13, 2021 at 23:29
  • Saved my life too. I also wonder how one would figure this out though?
    – Ruben
    Commented May 31, 2021 at 9:35
  • @Ruben You can have a look to my answer for a fix.
    – May.D
    Commented Jun 2, 2021 at 11:12
  • Hi @May.D, Seems like that might work too, but for now running 1.6.8 worked just fine. I already have my .exe file running.
    – Ruben
    Commented Jun 4, 2021 at 7:44
7

If you are running Windows you need to add these parameters to the command line (for the first time, after that they will be included in the generated spec file).

--hidden-import "pynput.keyboard._win32" --hidden-import "pynput.mouse._win32"

For Linux, use:

--hidden-import "pynput.keyboard._xorg" --hidden-import "pynput.mouse._xorg"

More information can be found in this Github issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.