![](/img/trans.png)
[英]How do I detect the current background color of the terminal in which my python script was invoked?
[英]How do I detect if my Python script is running in the 'Windows Terminal'
我可以提供這種方法:
is_windows_terminal = sys.platform == "win32" and os.environ.get("WT_SESSION")
但可能有一個更清潔的解決方案......
您可以獲取運行/生成並運行 python 腳本的進程的父進程 ID ( PID
),並在 windows CMD 的任務列表中搜索並查看此pid
屬於誰:
在您的 python 腳本中添加這些行
import psutil
my_father_pid = str(psutil.Process().ppid())
print my_father_pid # or print(my_father_pid) in python3
現在在任務列表中搜索您獲得的“my_father_pid”:
tasklist /v | findstr "<my_father_pid>"
我為這個問題創建了一個 package: https://pypi.org/project/AssertWT/
import assertwt
assertwt.is_wt() # True,False
is_wt
方法的源碼可以在github找到
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.