【发布时间】:2019-07-24 11:32:21
【问题描述】:
我想将以下代码从具有 python 2.7 的 MAC 导出到具有以下依赖项 (pynput) 的 Windows,该依赖项是从 pip 导入的。 我希望这个人能够在他们的终端中运行该文件而无需安装 pynput。 我尝试将文件转换为可执行文件,但它甚至无法在我的机器上运行。
代码如下:
import thread
import random
import time
from pynput.mouse import Listener, Controller, Button
mouse = Controller()
trigger = False
def on_click(x, y, button, pressed):
global trigger
if str(button) == "Button.middle" and pressed:
trigger = True
print "Middle button has been pressed"
if str(button) == "Button.middle" and pressed == False:
print "Middle button has been unpressed"
trigger = False
def loop_thread(threadName, delay):
while True:
time.sleep(delay)
if trigger == True:
sleep_time = random.uniform(0.02, 0.12)x
time.sleep(sleep_time)
mouse.click(Button.left, 1)
def listener_thread(threadName, delay):
with Listener(on_click = on_click) as listener:
listener.join()
try:
thread.start_new_thread( loop_thread, ("Thread-1", 0.025 ) )
thread.start_new_thread( listener_thread, ("Thread-2", 0.25, ) )
except:
print "Error: unable to start thread"
while 1:
pass
您知道是否有任何方法可以使 python 脚本跨平台并在所述脚本中包含依赖项?
【问题讨论】:
-
这可能会有所帮助,有办法。 :) docs.python-guide.org/shipping/freezing 您可能需要为每个发行版进行必要的编辑并冻结它。
标签: python python-2.7 cross-platform pynput