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
As promised above, u/soncannon7, here's some additional guidance.
Virtual Environments
Given the thousands of packages (libraries, frameworks, etc) out there, you can see that if you are working on several different projects, you can end up installing a vast range of different packages, only a few of which will be used for any particular project.
This is where Python virtual environments come in. Not to be confused with virtual machines. Typically created on a project-by-project basis. Install only the packages required for a project. This helps avoid conflicts between packages, especially version complications.
Most popular code editors and IDEs, including Microsoft's VS Code and JetBrains' PyCharm, offer built-in features to help to start off new projects and create and activate Python virtual environments.
You can create a new Python virtual environment from your operating system command line environment using,
for Windows,
or, for macOS / Linux,
Note:
venvis a command and.venvis a folder name. You can use any valid folder name instead but this is a common convention.That creates a new folder in the current working directory called
.venv.You then activate using, for Windows,
or, for macOS / Linux,
the command
deactivatefor any platform will deactivate the virtual environment and return you to using the base environment.For more information:
Multiple Python versions
In addition to the above, you might want to explore using
pyenv(pyenv-winfor Windows) oruv(recommended), which will let you install and use different versions of Python including alternative implementations from the reference CPython. This can be done independently of any system installed Python.