Crontab未执行脚本



下面是我的代码:

#!/usr/bin/python3
import pyautogui
import random
screen_width, screen_height = pyautogui.size()
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
pyautogui.moveTo(x, y)
print("Success!")

下面是crontab条目:

          • /图书馆/框架/Python.framework/版本/3.11/bin/python3/用户/tylerkivelson/桌面/python/mouse_move_test.py

在使用chmod +x mouse_move_test.py后,在终端执行。/mouse_move_test.py命令了:

Traceback (most recent call last):
File "./mouse_move_test.py", line 3, in <module>
import pyautogui
ModuleNotFoundError: No module named 'pyautogui'

,我认为这可能是为什么crontab不运行脚本的原因?我很迷茫

我猜这是Python版本不匹配的问题。

使用以下命令运行python脚本:

./mouse_move_test.py

您的shell打开该文件并在顶部找到这一行:

#!/usr/bin/python3

所以shell将使用这个python可执行文件来运行你的代码。

但是,您为/Library/Frameworks/Python.framework/Versions/3.11安装的特定版本的python安装了pyautogui模块。

因此,/Library/Frameworks/Python.framework/Versions/3.11/usr/bin/python3很可能是不同版本的python。

要解决这个问题,从python代码的顶部删除#!/usr/bin/python3行。

要测试您的脚本,您可以使用与crontab条目中相同的命令:

/Library/Frameworks/Python.framework/Versions/3.11/bin/python3 /Users/tylerkivelson/Desktop/python/mouse_move_test.py

最新更新