Python 解释器在 Powershell ISE 中崩溃 [英] Python Interpreter crashing in Powershell ISE

查看:91
本文介绍了Python 解释器在 Powershell ISE 中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have python 3 installed on my system and a path to the executable has been added to the PATH. When i inter python in Windows PowerShell (win8.1) it runs fine, however i'd like to use PowerShell ISE for the advanced features it has. However running python in PowerShell ISE crashes with the following log:

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.

I then can't enter anything and have to Ctrl+C to get back to PowerShell.

这里可能有什么问题?

推荐答案

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

PowerShell ISE isn't meant for running typical interactive console programs such as python.exe. It hides the console window and redirects stdout to a pipe. To see this in practice run the following in ISE:

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

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

Enter text in the console window, and you'll see the input echoed in the console, but output gets piped to ISE.

以下简要概述了 Windows 控制台应用程序.powershell.exe、cmd.exe 和 python.exe 都是作为控制台服务器(或主机)进程 conhost.exe 的客户端的控制台应用程序.宿主进程创建窗口并运行典型的 GUI 事件循环.当您从 GUI 应用程序(例如 explorer.exe)运行 python.exe 时,Windows 会执行 conhost.exe 的新实例,从而创建一个新的控制台窗口.当您从另一个控制台应用程序(例如 powershell.exe)运行 python.exe 时,默认行为是继承父级的控制台.

Here's some a brief overview of Windows console applications. powershell.exe, cmd.exe, and python.exe are all console applications that function as clients of a console server (or host) process, conhost.exe. The host process creates the window and runs the typical GUI event loop. When you run python.exe from a GUI application, such as explorer.exe, Windows executes a new instance of conhost.exe, which creates a new console window. When you run python.exe from another console application, such as powershell.exe, the default behavior is to inherit the console of the parent.

控制台 API 与连接的控制台主机进行通信.许多功能,例如 WriteConsole,需要一个句柄到控制台输入或屏幕缓冲区.如果你连接到控制台,特殊文件 CONIN$ 代表输入缓冲区,CONOUT$ 是当前屏幕缓冲区,CON可以根据它是打开读取还是写入来引用.(你可能在 cmd.exe 中看到过一个命令,比如 copy con somefile.txt.)

The console API communicates with the attached console host. Many of the functions, such as WriteConsole, require a handle to a console input or screen buffer. If you're attached to a console, the special file CONIN$ represents the input buffer, CONOUT$ is the current screen buffer, and CON can refer to either depending on whether it's opened for reading or writing. (You may have seen a command in cmd.exe such as copy con somefile.txt.)

Windows 进程具有三个用于标准 I/O 句柄的字段.对于控制台进程,StandardInput 默认为 CONIN$ 的句柄,StandardOutputStandardError 默认为<代码>CONOUT$.C 运行时库将这些作为标准FILE stdinstdoutstderr 使用文件描述符 0、1 和 2.而是设置为打开的文件或管道.

A Windows process has three fields used for standard I/O handles. For a console process StandardInput defaults to a handle for CONIN$, and StandardOutput and StandardError default to handles for CONOUT$. The C runtime library opens these as the standard FILE streams stdin, stdout, and stderr using file descriptors 0, 1, and 2. When starting a process any of the standard handles can instead be set to an open file or pipe.

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

While a process can attach to only one console at a time, multiple processes can be attached to a single console. However, usually only one process is active. In the case of powershell.exe, for example, after running python.exe its main thread is waiting in the background for python.exe to exit. (Note that this execution model fails badly if in python.exe you start another interactive console process and then exit, since now both the shell and the child process compete for access to the console.)

这篇关于Python 解释器在 Powershell ISE 中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆