Python Command-Line Program Specification
Python Command-Line Program Specification
Code to be run and command-line arguments to send to it are specified in the following ways in Python command lines:
scriptfile
Denotes the name of a Python script file to run as the main, topmost file of a program (e.g., python main.py runs the code in main.py). The script’s name may be an absolute or relative (to “.”) filename path, and is made available in sys.argv[0]. On some platforms, command lines may also omit the python component if they begin with a script file name and have no options for Python itself.
-c command
Specifies Python code (as a string) to run (e.g., python -c “print('spam' * 8)” runs a Python print operation). sys.argv[0] is set to '-c'.
-m module
Runs a module as a script: searches for module on sys.path and runs it as a top-level file (e.g., python -m pdb s.py runs the Python debugger module pdb located in a standard library directory, with argument s.py). module may also name a package (e.g., idlelib.idle). sys.argv[0] is set to the module’s full path name.
−
Reads Python commands from the standard input stream, stdin (the default); enters interactive mode if stdin is a “tty” (interactive device). sys.argv[0] is set to '−'.
arg*
Indicates that anything else on the command line is passed to the script file or command, and appears in the built-in list of strings sys.argv[1:].
If no scriptfile, command, or module is given, Python enters interactive mode, reading commands from stdin (and using GNU readline for input, if installed), and setting sys.argv[0] to (the empty string) unless invoked with option – in the preceding list.
Besides using traditional command lines at a system shell prompt, you can also generally start Python programs by clicking their filenames in a file explorer GUI; by calling functions in the Python standard library (e.g., os.popen()); by using program-launch menu options in IDEs such as IDLE, Komodo, Eclipse, and NetBeans; and so on.