Reputation: 14814
I need to check if my Python script is running inside the Windows Terminal (as opposed to the CMD.exe, Powershell, bash, etc.).
How can this be done?
Upvotes: 1
Views: 1084
Reputation: 1097
I have created a package for this problem: https://pypi.org/project/AssertWT/
import assertwt
assertwt.is_wt() # True,False
The source code for the is_wt
method can be found on github
Upvotes: 1
Reputation: 2950
you can get the parent process id (PID
) for the process that run/spawned and run your python script and search in the tasklist in windows CMD and see this pid
belongs to whom:
In you python script add those lines
import psutil
my_father_pid = str(psutil.Process().ppid())
print my_father_pid # or print(my_father_pid) in python3
Now search for 'my_father_pid` that you have get, in the task list:
tasklist /v | findstr "<my_father_pid>"
Upvotes: 0
Reputation: 14814
I can offer this approach:
is_windows_terminal = sys.platform == "win32" and os.environ.get("WT_SESSION")
But there may be a cleaner solution...
Upvotes: 3