Search code examples
pythonpyautogui

Pyautogui script only works in ide


I have this script

from pyautogui import *

while True:
    keyDown("w")
    sleep(2)
    keyUp("w")
    sleep(0.1)
    keyDown("s")
    sleep(2)
    keyUp("s")

and it works in pycharm, but when I run the script by clicking on it closes and does nothing.

why does it only work in the IDE?


Solution

  • Check if your PyCharm has a Virtual Environment set up where pyautogui is installed, this should be the case.

    You can then either run the script in your terminal inside the virtual environtment, or install pyautogui globally. I would recommend doing the first.

    You can find your PyCharm python interpreter in the Run/Debug Configuration next to your run button. If your python interpreter has something like pipenv, virtualenv, venv in its name, it is likely a virtual enviroment, as PyCharm defaults to creating one

    edit: If you really just want to click on it, you have to install pyautogui globally. Open a terminal, write:

    pip install pyautogui
    

    or

    pip3 install pyautogui
    

    depending on how your python is installed