Home > other >  How to get type of hosting window from inside Python?
How to get type of hosting window from inside Python?

Time:12-13

I would like to know which window is hosting the terminal running Python. In specific, I would like to distinguish between windows terminal and the old CMD console on Windows machine.

EDIT: I'm not sure I'm using correct words and there is an overload of words anyway. To be more specifc, I want to know the host window becaue they have different behaviours. Here's a photo of different windows, one of which Windows Terminal. powershell or cmd can be run in either of the windows, I'm interested in figuring out that window host.

enter image description here

CodePudding user response:

If you use the psutil and os packages, you can use

parent_pid = os.getppid()
print(psutil.Process(parent_pid).name())

to get the parent process' name.

CodePudding user response:

You could query WMI as I prefer to use OS tools (should work with psutil aswell, as mentioned by enter image description here

  • Related