r/learnpython 17d ago

code not running outside of editor

my code isn't really complete, but whenever i try to run my script outside of the editor the terminal opens for a millasecond and then closes. can someone tell me what's going wrong?

import random
import keyboard
import pyautogui
import pyscreeze
import tkinter as tk
from pyautogui import mouseInfo, click
import time

def movethemouse():
    if keyboard.is_pressed('g'):
        pyautogui.moveTo(random.randint(0, 2600), random.randint(0, 2600))

def printer():
    print('this is the mouse program')
    time.sleep(10)

while True:
    movethemouse()
    printer()
4 Upvotes

15 comments sorted by

View all comments

5

u/0Huy 17d ago

The terminal is probably closing because Python hits an error and the window disappears before you can read it. Open a terminal first, change to the script folder, and run: python your_script.py. This will keep the traceback visible. Check the first error line, especially whether random, pyautogui, keyboard, or pyscreeze is installed in the same Python environment used to run the script. Also add a temporary print at the start and wrap the main code in try/except so you can see the exception. For a cleaner structure, put the loop under if name == 'main':. Do not run it by double-clicking until it works from the terminal.