how to make my console in python not to close?

穿精又带淫゛_ 提交于 2021-01-28 12:05:21

问题


I'm making a application in python from Windows. When I run it in the console, it stops, shows an error, and closes. I can't see the error becase its too fast, and I can't read it. I'm editing the code with IDLE (the program that came with python when I instaled it), and when I run it with the python shell, there are no errors. I would run it from IDLE, but when I use the console, it has more features.

I don't know why this is happening. I need your help.


回答1:


Run your program from a Windows command prompt. That will not automatically close when the program finishes.

If you run your program by double-clicking on the .py file icon, then Windows will close the window when your program finishes (whether it was successful or not).




回答2:


Run the program from an already-open terminal. Open a command prompt and type:

python myscript.py

For that to work you need the python executable in your path. Just check on how to edit environment variables on windows, and add C:\PYTHON26 (or whatever directory you installed python to).When the program ends, it'll drop you back to the CMD windows prompt instead of closing the window.Add code to wait at the end of your script. Adding ...

raw_input()

... at the end of the script makes it wait for the ENTER key. That method is annoying because you have to modify the script, and have to remember removing it when you're done.




回答3:


Create a text file in the program directory i.e. wherever your script is located. Change the extension to .bat for example text.bat. Then edit the text file and write:

python main.exe
pause

Now you can run the program without typing into the command console by double clicking the bat file, and the console window will not close.



来源:https://stackoverflow.com/questions/11876545/how-to-make-my-console-in-python-not-to-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!