Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if Python is running in Git Bash terminal, or Windows cmd?

I'd like to give my Python scripts the ability to detect whether it was executed in a Git Bash terminal, or the Windows cmd command line interface. For example, I'm trying to write a function to clear the terminal (regardless of which terminal it is), e.g. echoes the clear command if in Git Bash, or cls if in cmd.

I've tried using sys.platform to detect this, but it returns win32 regardless of which type of terminal it was ran in.

like image 397
mbg117 Avatar asked Jan 21 '26 07:01

mbg117


1 Answers

Please try using os and psutil modules.

For example,

import os, psutil  # Get the parent process name. 
pprocName = psutil.Process(os.getppid()).name()

Then you can have your logic depending on the shell.

Additionally, you may want to check https://www.geeksforgeeks.org/clear-screen-python/

like image 180
programandoconro Avatar answered Jan 22 '26 20:01

programandoconro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!