我刚刚开始学习Python。当我在Windows上执行一个python脚本文件时,输出窗口出现但立即消失。我需要它停留在那里,这样我就可以分析我的输出。我怎么才能让它一直开着?
当前回答
cmd /k是打开任何控制台应用程序(不仅仅是Python)的典型方式,其中控制台窗口在应用程序关闭后仍然存在。我能想到的最简单的方法是按下Win+R,键入cmd /k,然后拖放你想要的脚本到运行对话框。
其他回答
我也遇到过类似的问题。在notepad++中,我曾经使用命令:C:\Python27\python.exe "$(FULL_CURRENT_PATH)"在代码终止后立即关闭cmd窗口。 现在我使用cmd /k c:\Python27\python.exe "$(FULL_CURRENT_PATH)"它保持cmd窗口打开。
你有几个选择:
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.
最简单的方法:
import time
#Your code here
time.sleep(60)
#end of code (and console shut down)
这将使代码保持1分钟,然后关闭它。
在windows 10上插入以下语句:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
奇怪,但对我很管用!(当然是和input()一起放在末尾)
除了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和程序的完整目录。
推荐文章
- 如何在Windows命令提示符下运行.sh ?
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- defaultdict的嵌套defaultdict
- 构造tkinter应用程序的最佳方法?
- 如何在Python中逐行打印字典?
- 当试图运行Python脚本时,“ImportError:没有命名模块”
- pylab和pyplot的区别是什么?
- Argparse:确定使用了哪个子解析器
- django导入错误-没有core.management模块
- 在芹菜中检索队列中的任务列表
- 使用beautifulsoup提取属性值
- 如何禁用标准错误流的日志记录?
- 用Matplotlib在Python中绘制时间
- 类中的Python装饰器
- 在Python中锁定文件