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?
I can offer this approach:
is_windows_terminal = sys.platform == "win32" and os.environ.get("WT_SESSION")
But there may be a cleaner solution...
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>"
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.