How to determine if Python script was run via command line?

前端 未结 11 1697
心在旅途
心在旅途 2020-12-15 03:58

Background

I would like my Python script to pause before exiting using something similar to:

raw_input(\"Press enter to close.\")

but

11条回答
  •  粉色の甜心
    2020-12-15 04:30

    What I wanted was answered here: Determine if the program is called from a script in Python

    You can just determine between "python" and "bash". This was already answered I think, but you can keep it short as well.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    import psutil
    import os
    
    ppid = os.getppid() # Get parent process id
    print(psutil.Process(ppid).name())
    

提交回复
热议问题