我刚刚开始学习Python。当我在Windows上执行一个python脚本文件时,输出窗口出现但立即消失。我需要它停留在那里,这样我就可以分析我的输出。我怎么才能让它一直开着?
当前回答
你有几个选择:
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.
其他回答
一个非常迟的回答,但我创建了一个名为pythonbat.bat的Windows批处理文件,其中包含以下内容:
python.exe %1
@echo off
echo.
pause
然后指定pythonbat.bat作为.py文件的默认处理程序。
现在,当我在文件资源管理器中双击一个.py文件时,它会打开一个新的控制台窗口,运行Python脚本,然后暂停(保持打开),直到我按下任何键……
无需更改任何Python脚本。
我仍然可以打开一个控制台窗口,并指定python myscript.py,如果我想…
(我刚刚注意到@maurizio已经发布了这个确切的答案)
为了保持窗口打开,我同意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,traceback
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
只需在实际代码之后编写上面的代码。如。我从用户输入和打印在控制台,因此我的代码将看起来像这样->
`import sys,traceback
nam=input("enter your name:")
print("your name is:-{}".format(nam)) #here all my actual working is done
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`
我也遇到过类似的问题。在notepad++中,我曾经使用命令:C:\Python27\python.exe "$(FULL_CURRENT_PATH)"在代码终止后立即关闭cmd窗口。 现在我使用cmd /k c:\Python27\python.exe "$(FULL_CURRENT_PATH)"它保持cmd窗口打开。
除了input和raw_input,你还可以使用一个无限while循环,像这样: while True: pass (Python 2.5+/3)或while 1: pass(所有版本的Python 2/3)。不过,这可能需要计算能力。
还可以从命令行运行程序。在命令行中输入python (Mac OS X终端),它应该是python 3。(你的Python版本)如果它没有显示你的Python版本,或者说Python:命令没有找到,查看更改PATH值(环境值,上面列出)/输入C:\(Python文件夹\ Python .exe。如果成功,输入python或C:\(python安装)\python.exe和程序的完整目录。
推荐文章
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 如何运行一个PowerShell脚本而不显示窗口?
- PowerShell:仅为单个命令设置环境变量
- 在Python中重置生成器对象
- 用Python构建最小的插件架构
- model.eval()在pytorch中做什么?
- Tensorflow 2.0:模块“Tensorflow”没有属性“Session”
- 从环境文件中读入环境变量
- 在OSX 10.11中安装Scrapy时,“OSError: [Errno 1]操作不允许”(El Capitan)(系统完整性保护)
- 如何删除熊猫数据帧的最后一行数据
- 我如何在熊猫中找到数字列?
- 检查pandas数据框架索引中是否存在值