Python 解释器在 Powershell ISE 中崩溃

Python Interpreter crashing in Powershell ISE

我的系统上安装了 python 3 并且可执行文件的路径已添加到 PATH 中。当我在 Windows PowerShell (win8.1) 中插入 python 时,它运行良好,但是我想使用 PowerShell ISE 来获得它具有的高级功能。但是,PowerShell ISE 中的 运行 python 崩溃并显示以下日志:

python : Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
In Zeile:1 Zeichen:1
+ python
+ ~~~~~~
    + CategoryInfo          : NotSpecified: (Python 3.4.3 (v...ntel)] on win32:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Type "help", "copyright", "credits" or "license" for more information.
>>> 

(抱歉部分是德语)

然后我无法输入任何内容,必须按 Ctrl+C 才能返回到 PowerShell。

这可能是什么问题?

PowerShell ISE 不适用于 运行 典型的交互式控制台程序,例如 python.exe。它隐藏控制台 window 并将 stdout 重定向到管道。要在实践中看到这一点 运行 ISE 中的以下内容:

python.exe -i -c "import ctypes; ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 5)"

在控制台中输入文本 window,您会看到输入在控制台中回显,但输出会通过管道传输到 ISE。


这里有一些 Windows console applications 的简要概述。 powershell.exe、cmd.exe 和 python.exe 都是控制台应用程序,充当控制台服务器(或主机)进程的客户端,conhost.exe。主机进程创建 window 和 运行s 典型的 GUI 事件循环。当您从 GUI 应用程序 运行 python.exe 时,例如 explorer.exe,Windows 执行 conhost.exe 的新实例,这将创建一个新的控制台 window.当您从另一个控制台应用程序 运行 python.exe 时,例如 powershell.exe,默认行为是继承父控制台。

console API communicates with the attached console host. Many of the functions, such as WriteConsole, require a handle 到控制台输入或屏幕缓冲区。如果您连接到控制台,特殊文件 CONIN$ 代表输入缓冲区,CONOUT$ 是当前屏幕缓冲区,而 CON 可以参考其中任何一个,具体取决于它是否打开以供阅读或写作。 (您可能已经在 cmd.exe 中看到过命令,例如 copy con somefile.txt。)

一个Windows进程有三个用于标准I/O句柄的字段。对于控制台进程,StandardInput 默认为 CONIN$ 的句柄,而 StandardOutputStandardError 默认为 CONOUT$ 的句柄。 C 运行time 库使用文件描述符 0、1 和 2 作为标准 FILE streams stdinstdoutstderr 打开它们。 process 任何标准句柄都可以改为设置为打开的文件或管道。

虽然一个进程一次只能连接到一个控制台,但多个进程可以连接到一个控制台。但是,通常只有一个进程处于活动状态。例如,在 powershell.exe 的情况下,在 运行 宁 python.exe 之后,其主线程在后台等待 python.exe 退出。 (请注意,如果在 python.exe 中启动另一个交互式控制台进程然后退出,此执行模型会严重失败,因为现在 shell 和子进程都在竞争对控制台的访问权。)