Reputation: 95
I code in PyCharm and went into C:\Users\MyUser\PyCharmProjects\MyFile and from there went into cmd in that directory, executed pyinstaller --onefile -w main.py. This worked perfect. I grabbed my main.exe from dist and put it into MyFile and executed it but upon doing so it brought up the following error:
Traceback (most recent call last):
File "main.py", line 2, in <module>
from pynput.mouse import Controller, Button
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\keyboard\__init__.py, line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[4752] Failed to execute script main
The purpose of my code is to move around your mouse rapidly until you hold down a, b, and c. Just a fun practical joke. Here is a copy of it:
import keyboard
from pynput.mouse import Controller, Button
import random
import tkinter as tk
root = tk.Tk()
mouse = Controller()
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
while True:
xVar = random.randint(0, screen_width)
yVar = random.randint(0, screen_height)
mouse.position = (xVar, yVar)
if keyboard.is_pressed("a") and keyboard.is_pressed("b") and keyboard.is_pressed("c"):
break
I've honestly tried everything. I can't figure out what is going wrong with it. Any help would be appreciated. Thanks!
Upvotes: 1
Views: 338
Reputation: 15088
You have to add --hidden-import="pynput.keyboard._win32" --hidden-import "pynput.mouse._win32"
, so pyinstaller
can include the backend that pynput
uses. So something like:
pyinstaller --hidden-import="pynput.keyboard._win32" --hidden-import="pynput.mouse._win32" app.py
You can also add this to your spec file.
hiddenimports=["pynput.keyboard._win32", "pynput.mouse._win32"]
If you are using macOS then replace '_win32' with '_darwin' and if linux, replace with '_xorg'.
The other method is to fallback to a previous version which is not recommended, but just incase you want it. First pip uninstall pynput
then:
pip install pynput==1.6.8
Upvotes: 3