首页 > 解决方案 > 将具有依赖关系的 python 脚本导出到其他操作系统

问题描述

我想将以下代码从带有 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 脚本跨平台并在所述脚本中包含依赖项?

标签: pythonpython-2.7cross-platformpynput

解决方案


可执行文件需要在相同的操作系统上运行才能工作,所以没有冻结、Cython 或 nuitka。您可以尝试pex,我认为它适用于整个操作系统,您将不得不发送您的程序和 pex 文件。


推荐阅读