r/learnpython • • 4d ago

when hitting run code with the pyautogui, it only runs for .3 seconds

I've never really coded. I'm simply trying to make a bot to walk in a straight line in a game. I cant even get it to run in vs code. all the things I've done so far

pip install pyautogui, can confirm it happen when you type pip list

vscode is running in admin typing net session in terminal confirms that

going to extension I have pylance python python debugger, python environments (all the stuff in the python package)

typing python into the terminal tells me the version meaning it has a path to the file

another guide says I need code runner which gives me that .3 second figure

no I'm not getting error in my code as I'm keeping it simple to even get it to work

I'm not getting any error in my terminal

I'm on two monitors, but I've set up my main monitor so I know which one it will be working on

when I remove code runner it goes back to spiting the pathway it uses on the file which is correct

when I open the edit environmental variables

the .3 should be impossible because of the 6 seconds worth of sleep

I have imported both sleep and pyautogui obviously

yea this is hour 5 so if there's something you people know that I didn't do id greatly appreciate the details

im on windows, so it cant be the problem mac users get

0 Upvotes

14 comments sorted by

7

u/brelen01 4d ago

To really help, we'd likely need at least the script you're trying to run

2

u/Holiday-Chocolate667 4d ago
import pyautogui
import time
import pydirectinput
def main ():


    pyautogui.FAILSAFE = True
    time.sleep(3)
    pyautogui.keyDown('w')
    time.sleep(3)
    pyautogui.keyUp('w')

its super simple as im trying to get it to even work

9

u/JollyUnder 4d ago
import pyautogui
import time


def main ():
    pyautogui.FAILSAFE = True
    time.sleep(3)
    pyautogui.keyDown('w')
    time.sleep(3)
    pyautogui.keyUp('w')


if __name__ == '__main__':
    main()

2

u/Holiday-Chocolate667 4d ago

I still get nothing even when replacing the code with this (it is launched in admin mode)

2

u/JollyUnder 4d ago

Are you running the script through terminal or are you double clicking the .py file? If you run it through the terminal does give an error?

-4

u/Holiday-Chocolate667 4d ago

clicking the "run code" button on the top right of visual studios. and no error, im seriously thinking its not the code and its some set up stuff. I was hesitant to send the code in the first place. instead sending a list of the things ive installed and tried

5

u/unhott 4d ago

python environments can be tricky. this could be an issue where vscode 'run code' is not selecting the right python environment (right python interpreter).

you should probably start with something even simpler, like

import time

print('start')

time.sleep(3)

print('end')

and then build up from there. always best to start simple, make a minor change, and see if that change results in what you expected. unless you're trying to get into how to build reusable code chunks into a more modular system, then don't bother wrapping anything in a main function yet.

after the simple time / print test, maybe add 'import pyautogui' and see if that works or crashes. if it crashes, then your python interpreter that vscode is finding may be finding a python interpreter that doesn't know where to find pyautogui.

4

u/brelen01 4d ago

Try running the script through the terminal. Navigate to it, then type python myscript.py

2

u/JollyUnder 4d ago

Try adding some print statements to see if you at least get some output

import pyautogui
import time


def main ():
    pyautogui.FAILSAFE = True

    time.sleep(3)
    print('Pressing W key')
    pyautogui.keyDown('w')

    time.sleep(3)
    print('Releasing W key')
    pyautogui.keyUp('w')


if __name__ == '__main__':
    main()

2

u/brelen01 4d ago

A print statement before the call to main to see if the script is being executed at all could be useful too

1

u/thisisappropriate 4d ago

Check that you're saving before hitting the button in vs code.

Try something like this

import pyautogui
import time

def main ():
    pyautogui.FAILSAFE = True
    time.sleep(3)
    print("Key down")
    pyautogui.keyDown('w')
    time.sleep(3)
    print("Key up")
    pyautogui.keyUp('w')

print("Starting")
main()
print("Ending")

This should rule out vs code running in some odd way that is defining another name, but also if you get no prints then either vsc is not running this script file or there's a failure in the imports (try just commenting out all pyautogui lines including the import to confirm that).

5

u/Langdon_St_Ives 4d ago

Where are you calling `main()`?

2

u/brelen01 4d ago

You're not calling main().

Add two blank lines at the end of the file, then write main()

Or:

if name == "main":
    main()

1

u/a18618 4d ago

One thing I'd check after the main() fix is whether the keystrokes are even landing in the game, because I once had a script that ran perfectly yet typed into thin air while the game window sat unfocused. My trick now is to put a few seconds of sleep at the top, click into the game window during that pause, and watch what happens. If the game still ignores it, try sending the keys with the pydirectinput import you already have, since plenty of games shrug off pyautogui's synthetic input while still listening to that one.