Search code examples
pythonterminalpython-idlesys

Is there a way to check what you are running a python program in?


I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules: works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'?


Solution

  • You can try using psutil and os

    import psutil
    import os
    if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
        print("in cmd")
    

    Using idle it returned 'pythonw.exe' which shows this works.