简体   繁体   中英

Some Python scripts CMD windows closes immediately

Some of my python (3.6) scripts will close immediately when run. Others, however, do not. The script in question will copy a selected text and look up the word on dictionary.com. When I run the script from Pycharm, it works as intended. However, when I run it from the search bar or double-click the file from within the folder, it immediately closes. I have tried using input("something") and time.sleep(1) but with no luck. I also have success with opening a command window and typing python dictionary.py and enter.

This is what the body looks like:

import pyperclip
import keyboard
import pyautogui
import time


def lookup():
    prev = pyperclip.paste()
    time.sleep(.1)
    pyautogui.hotkey('ctrl', 'c')
    time.sleep(.1)
    word = pyperclip.paste()
    url = f"https://www.dictionary.com/browse/{word}?s=t"
    webbrowser.open(url)
    pyperclip.copy(prev)


def switch_back():
    pyautogui.keyDown('ctrl')
    pyautogui.press("w")
    pyautogui.keyUp('ctrl')
    pyautogui.keyDown('alt')
    pyautogui.press('tab')
    pyautogui.keyUp('alt')


print("Welcome to dictionary lookup!")
print("Press F2 to lookup a word on www.dictionary.com")
word = ""
keyboard.add_hotkey('f2', lookup)
keyboard.add_hotkey('f1', switch_back)
while word is not "stop":
    pass

Some of my other scripts that ask for input will not close immediately. The one I tried does not use any imports, so I'm theorizing this might have something to do with it. The reason I want this is that I want to be able to press windows button, type name of script and press enter. Rather than to find the folder, open a command window and type the whole thing out.

It seems the problem has been resolved. A time ago I tried installing a newer version of python (3.8). I have not been using this version (because I could not figure out how to change the python interpreter in Pycharm). I tried uninstalling it, and it seems to be working now.

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