问题
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.
>>>
(sorry its partly in german)
I then can't enter anything and have to Ctrl+C to get back to PowerShell.
What might be the issue here?
回答1:
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)"
Enter text in the console window, and you'll see the input echoed in the console, but output gets piped to ISE.
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.
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
.)
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.
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.)
来源:https://stackoverflow.com/questions/31548465/python-interpreter-crashing-in-powershell-ise