我刚刚开始学习Python。当我在Windows上执行一个python脚本文件时,输出窗口出现但立即消失。我需要它停留在那里,这样我就可以分析我的输出。我怎么才能让它一直开着?


当前回答

创建一个像dontClose()这样的函数或带有while循环的函数:

import time

def dontClose():
    n = 1
    while n > 0:
        n += 1
        time.sleep(n)
        

然后在代码之后运行函数。例如:

print("Hello, World!")
dontClose()

其他回答

创建一个像dontClose()这样的函数或带有while循环的函数:

import time

def dontClose():
    n = 1
    while n > 0:
        n += 1
        time.sleep(n)
        

然后在代码之后运行函数。例如:

print("Hello, World!")
dontClose()

一个非常迟的回答,但我创建了一个名为pythonbat.bat的Windows批处理文件,其中包含以下内容:

python.exe %1
@echo off
echo.
pause

然后指定pythonbat.bat作为.py文件的默认处理程序。

现在,当我在文件资源管理器中双击一个.py文件时,它会打开一个新的控制台窗口,运行Python脚本,然后暂停(保持打开),直到我按下任何键……

无需更改任何Python脚本。

我仍然可以打开一个控制台窗口,并指定python myscript.py,如果我想…

(我刚刚注意到@maurizio已经发布了这个确切的答案)

Go here and download and install Notepad++ Go here and download and install Python 2.7 not 3. Start, Run Powershell. Enter the following. [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User") Close Powershell and reopen it. Make a directory for your programs. mkdir scripts Open that directory cd scripts In Notepad++, in a new file type: print "hello world" Save the file as hello.py Go back to powershell and make sure you are in the right directory by typing dir. You should see your file hello.py there. At the Powershell prompt type: python hello.py

你可以使用-i选项启动python,或者设置环境变量PYTHONINSPECT=x。从文档中可以看出:

运行脚本后进行交互检查;强制执行提示符甚至 如果stdin看起来不是终结符;还PYTHONINSPECT = x

所以当你的脚本崩溃或完成时,你会得到一个python提示符,你的窗口不会关闭。

在windows 10上插入以下语句:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

奇怪,但对我很管用!(当然是和input()一起放在末尾)