Here is my code:
#!/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!")
Here is the crontab entry:
-
-
-
-
- /Library/Frameworks/Python.framework/Versions/3.11/bin/python3 /Users/tylerkivelson/Desktop/python/mouse_move_test.py
-
-
-
I did the ./mouse_move_test.py command in the terminal after using the chmod +x mouse_move_test.py and got:
Traceback (most recent call last):
File "./mouse_move_test.py", line 3, in <module>
import pyautogui
ModuleNotFoundError: No module named 'pyautogui'
and I think this might be the reason why the crontab won't run the script? I am very lost
I'm going to guess that this is a Python version mismatch issue.
When you run your python script with this command:
Your shell opens that file and finds this line at the top:
So the shell is going to use that python executable to run your code.
However, you installed the pyautogui module for the specific version of python installed at
/Library/Frameworks/Python.framework/Versions/3.11.So, it's very likely the case that
/Library/Frameworks/Python.framework/Versions/3.11and/usr/bin/python3are different versions of python.To fix this, remove the
#!/usr/bin/python3line from the top of your python code.To test your script, you can use the same command that is in the crontab entry: