Search code examples
pythonmsys2

Differentiate between MSYS2 & Powershell/Command Prompt


I'm writing a python project which is not going to work on PowerShell or Command Prompt, but it does work on MSYS2.

Is there any way to differentiate between MSYS2 and PowerShell/Command Prompt?

I tried os.name and sys.version, but both return same result between these 3.


Solution

  • Combining Abner Ham's answer with shellingham module:

    import shellingham
    
    def provide_default():
        if 'MSYSTEM' in os.environ:
            return 'bash'
        elif 'PSModulePath' in os.environ:
            return 'powershell'
        elif 'SHELL' in os.environ:
            return 'bash'
        else:
            return 'cmd'
    
    try:
        shell = shellingham.detect_shell()
    except shellingham.ShellDetectionFailure:
        shell = provide_default()
    if shell != 'bash':
        print(f"Shell detected: {shell}")
        print("Please use bash shell (Linux, MacOS, WSL, MSYS2, etc.) to run this script.")
        return