简体   繁体   中英

Stop program from closing in python

So I made a program that uses the pyautogui module to type "Hello" over and over again in a while loop and press enter. It's not a console output. It works perfectly fine when I launch it from the pycharm editor but when I open it from the folder or convert to an exe it just closes instantly. Why does the program close when I try opening it from the file explorer or after converting it to an exe? I'm new to python, thanks for any help!

import pyautogui
import time

message = "Hello"

time.sleep(5)

while True:
    pyautogui.typewrite(message)
    pyautogui.press("enter")
    time.sleep(37)

After pressing Run I have 5 seconds to switch to a notepad and it will start typing "Hello" and enter every 37 seconds.

For anyone having the same problem on a Mac, you need to go to your security preferences.
Settings > Security & Privacy > Privacy > Accessibility
and allow that specific application to access your computer by pressing the + icon. However, in the question, you stated that you are using .exe files, so I will assume you are on Windows. I don't use Windows, but for people reading this question in the future on Mac, I hope this helps.

I have finally solved the problem!!

I ran my python script via the windows command prompt. The error it showed me was an error with a module named pyautogui. The problem was that my module was installed with the wrong version. Using "pip3" instead of "pip" install pyautogui fixed the problem for me and now I can open and use my script directly from the file explorer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM