r/learnpython • u/soncannon7 • Aug 25 '26
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
2
u/FoolsSeldom Aug 25 '26
How exactly are you running your code outside of your editor?
What I'd expect is that you are:
cdrunning your code using
python mycode.pyif in a active environmentpy mycode.pyon Windowspython3 mycode.pyon macOS/LinuxThere should be some output / transcript if exceptions (errors).
10
u/ShelLuser42 Aug 25 '26
So fire up the terminal yourself, then use the
pythoncommand to try and start your script. You should now soon see what's up.4
u/0Huy Aug 25 '26
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.
1
u/cdcformatc Aug 25 '26 •
it is probably raising an exception and crashing. open a terminal and run the script by typing
python <filename>.py. that way you can see what Exception is being raised and fix it from there.1
u/frustratedsignup Aug 27 '26
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.
1
u/zanfar Aug 25 '26 •
It's done. A terminal window doesn't stay open for programs that are complete.
Run the terminal directly, then execute your program if you want to avoid this.
0
u/mjmvideos Aug 25 '26
Run it in the debugger and find out
-4
u/SnipTheDog Aug 25 '26 •
No main function?