我刚刚开始学习Python。当我在Windows上执行一个python脚本文件时,输出窗口出现但立即消失。我需要它停留在那里,这样我就可以分析我的输出。我怎么才能让它一直开着?
当前回答
我也遇到过类似的问题。在notepad++中,我曾经使用命令:C:\Python27\python.exe "$(FULL_CURRENT_PATH)"在代码终止后立即关闭cmd窗口。 现在我使用cmd /k c:\Python27\python.exe "$(FULL_CURRENT_PATH)"它保持cmd窗口打开。
其他回答
最简单的方法:
your_code()
while True:
pass
当您打开窗口时,它不会关闭,直到您关闭提示符。
你有几个选择:
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 prompt instead of closing the window. Add code to wait at the end of your script. For Python2, 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. Specially annoying when testing other people's scripts. For Python3, use input(). Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.
为了保持窗口打开,我同意Anurag的观点,这就是我为简短的计算类型程序保持窗口打开所做的。
这只会显示一个没有文本的游标:
raw_input()
下一个例子会给你一个明确的消息,程序已经完成,而不是等待程序内的另一个输入提示:
print('You have reached the end and the "raw_input()" function is keeping the window open')
raw_input()
注意! 在python 3中,没有raw_input(),只有 输入()。 (2)使用单引号表示字符串;否则,如果你在任何东西周围输入双精度,比如 "raw_input()",它会认为它是一个函数,变量等,而不是文本。
在下一个例子中,我使用双引号,但它不起作用,因为它认为“the”和“function”之间的引号中有一个断点,尽管当你阅读它时,你自己的大脑可以完全理解它:
print("You have reached the end and the "input()" function is keeping the window open")
input()
希望这能帮助到那些刚开始学习,还不知道计算机是如何思考的人。这可能需要一段时间。: o)
试试这个,
import sys
stat='idlelib' in sys.modules
if stat==False:
input()
这只会停止控制台窗口,而不是IDLE窗口。
让窗户一直开着的简单方法:
counter = 0
While (True):
If (counter == 0):
# Code goes here
counter += 1
计数器是这样代码就不会重复自己。
推荐文章
- 使用Conda进行批量包更新
- Ipython笔记本清除单元格输出代码
- ImportError: numpy.core.multiarray导入失败
- 有办法在Python中使用PhantomJS吗?
- 如何在Python中将if/else压缩成一行?
- 如何在Python 3中使用pip。Python 2.x
- 如何让IntelliJ识别常见的Python模块?
- Django:“projects”vs“apps”
- 如何列出导入的模块?
- 转换Python程序到C/ c++代码?
- 如何从gmtime()的时间+日期输出中获得自epoch以来的秒数?
- 在python模块文档字符串中放入什么?
- 我如何在Django中过滤一个DateTimeField的日期?
- 在Python中用索引迭代列表
- -e,——editable选项在pip install中什么时候有用?