Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Background

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

raw_input("Press enter to close.")

but only if it is NOT run via command line. Command line programs shouldn't behave this way.

Question

Is there a way to determine if my Python script was invoked from the command line:

$ python myscript.py

verses double-clicking myscript.py to open it with the default interpreter in the OS?

like image 726
Jace Browning Avatar asked Mar 23 '12 12:03

Jace Browning


People also ask

How do you check if a Python script is running or not?

I usually use ps -fA | grep python to see what processes are running. The CMD will show you what python scripts you have running, although it won't give you the directory of the script.

How do I test Python code in CMD?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I run a Python script from the command line with example?

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file, like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard and that's it.

Can I use CMD for Python?

But we can also run python programs on CMD or command prompt as CMD is the default command-line interpreter on Windows. But there's a need to set up the environment variable in windows to use python on the command-line.


3 Answers

If you're running it without a terminal, as when you click on "Run" in Nautilus, you can just check if it's attached to a tty:

import sys
if sys.stdin and sys.stdin.isatty():
    # running interactively
    print("running interactively")
else:
    with open('output','w') as f:
        f.write("running in the background!\n")

But, as ThomasK points out, you seem to be referring to running it in a terminal that closes just after the program finishes. I think there's no way to do what you want without a workaround; the program is running in a regular shell and attached to a terminal. The decision of exiting immediately is done just after it finishes with information it doesn't have readily available (the parameters passed to the executing shell or terminal).

You could go about examining the parent process information and detecting differences between the two kinds of invocations, but it's probably not worth it in most cases. Have you considered adding a command line parameter to your script (think --interactive)?

like image 134
Eduardo Ivanec Avatar answered Oct 08 '22 00:10

Eduardo Ivanec


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())
like image 9
HauiB Avatar answered Oct 08 '22 00:10

HauiB


I don't think there's any reliable way to detect this (especially in a cross-platform manner). For example on OS X, when you double-click a .py file and it tuns with "Python Launcher", it runs in a terminal, identically to if you execute it manually.

Although it may have other issues, you could package the script up with something like py2exe or Platypus, then you can have the double-clickable icon run a specific bit of code to differentiate (import mycode; mycode.main(gui = True) for example)

like image 5
dbr Avatar answered Oct 08 '22 02:10

dbr