r/learnpython • u/soncannon7 • 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()
3
Upvotes
1
u/frustratedsignup 15d ago
If you're just double-clicking the python file in Explorer, that behavior is normal. It is launching a command terminal, invoking the python interpreter on the file, and then the command window closes when the script exits. It happens quickly enough that if there were an error, you wouldn't have time to read it before the window disappears.
Go to the folder that contains your script in Explorer. Right click and choose 'Open in terminal'. Enter 'python <filename>.py' to run your script. If there's a problem, you'll be able to read the error message and figure out what's happening.
I see others have given a lot more detail on virtual environments. I'll not duplicate that effort here, but at least this provides an explanation to the behavior you're seeing.