1

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?

3 Answers 3

3

I can offer this approach:

is_windows_terminal = sys.platform == "win32" and os.environ.get("WT_SESSION")

But there may be a cleaner solution...

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.