将具有依赖性的python脚本导出到其他操作系统

时间:2019-03-02 18:05:57

标签: python python-2.7 cross-platform pynput

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

1 个答案:

答案 0 :(得分:0)

可执行文件需要在相同的操作系统上运行才能工作,因此不要冻结,Cython或nuitka。您可以尝试pex,我认为它在整个OS上都可以使用,您将必须附带程序和pex文件。

相关问题