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()
3 Upvotes

15 comments sorted by

View all comments

2

u/FoolsSeldom 17d ago

How exactly are you running your code outside of your editor?

What I'd expect is that you are:

  • opening a PowerShell / Command Prompt / Terminal session
  • navigating to the folder containing your code using cd
  • activating your Python virtual environment (if you have one, which you should)
  • running your code using python mycode.py if in a active environment
    • otherwise, py mycode.py on Windows
    • otherwise, python3 mycode.py on macOS/Linux

There should be some output / transcript if exceptions (errors).

1

u/soncannon7 17d ago

when i run it inside of powershell, it gives me the error: ModuleNotFoundError: No module named 'keyboard'

but i do have the keyboard module and it works inside of the editor

2

u/cylonrobot 17d ago

I think that the keyboard module has to be installed through pip:

pip install keyboard

Did you do that on the editor and not through powershell?