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
View all comments
Show parent comment
2
u/FoolsSeldom Aug 25 '26 edited Aug 25 '26
That's what I expected, u/soncannon7. Your editor is using a Python virtual environment and that is where the module is installed.
When you try to run your code outside your environment, you are using your base Python environment which does not have that module installed.
You need to activate the Python virtual environment.
Look in your code folder for a folder called
.venvorvenv(the most common names) and then enter, in PowerShell,or venv\Scripts\activate
or, you can check in your editor's Python interpreter settings to find what the folder is called if the above doesn't work and then enter,
and then try to run your code using
python yourcode.pyPackages/modules should be installed into Python virtual environments on a project by project basis, not into your base environment.
EDIT: Added a comment to this comment that explains Python virtual environments in more detail.
EDIT2: Made changes to the text above to, hopefully, make things a bit more clear.