Featureintro
Using the Python Interpreter
Also called python3, python command, py launcher, interactive mode, script mode, python -c, python -m, python -i, exit status, __main__.py.
The python3 program reads Python source and runs it: from a prompt, a script file, standard input, a -c string or a module named with -m.
- Language
- Python
Synopsis
import subprocess
import sys
def python(*args, stdin=None):
result = subprocess.run([sys.executable, *args], input=stdin,
capture_output=True, text=True)
return result.returncode, result.stdout
print(python('-c', 'print(6 * 7)'))
print(python('-', stdin='print("read from stdin")\n'))
print(python('-m', 'json.tool', '--compact', stdin='{"a": [1, 2]}'))
print(python('-c', 'raise SystemExit(3)'))
(0, '42\n')
(0, 'read from stdin\n')
(0, '{"a":[1,2]}\n')
(3, '')
sys.executable is the interpreter that runs this example. It is started four times: with a -c string, with a script on standard input, with a standard module run as a program, and with code that exits with status 3. Each run returns its exit status and what it printed.
Semantics
r1. The interpreter is a program, usually named with its version. The tutorial says it "is usually installed as /usr/local/bin/python3.15 on those machines where it is available", and that "other places are possible".1 "On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously installed Python 2.x executable."1 On Windows, "If you have the py.exe launcher installed, you can use the py command."1
r2. "The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file."1 A script can also be "a directory containing a main.py file, or a zipfile containing a main.py file".2
r3. Two more ways take code from the command line. python -c command "executes the statement(s) in command, analogous to the shell’s -c option."1 python -m module "executes the source file for module as if you had spelled out its full name on the command line."1
r4. "In non-interactive mode, the entire input is parsed before it is executed."2 A syntax error anywhere in a script stops the script before its first line runs.
r5. In interactive mode the interpreter prompts with the primary prompt >>> and, for continuation lines, the secondary prompt ....1 "Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status."1 quit() also exits.1
r6. -i runs a script, a -c string or a module and then enters interactive mode. "Interactive mode will start even when sys.stdin does not appear to be a terminal."2 "This can be useful to inspect global variables or a stack trace when a script raises an exception."2
r7. A program ends with an exit status. sys.exit and SystemExit take "an integer giving the exit status (defaulting to zero)", and "zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like."3 "If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1."3 Because the exit is an exception, "Cleanup actions specified by finally clauses of try statements are honored".3
Examples
The whole script is parsed first
import subprocess
import sys
source = 'print("first line")\nprint("unclosed"\n'
result = subprocess.run([sys.executable, '-'], input=source,
capture_output=True, text=True)
print(result.returncode, repr(result.stdout))
print(result.stderr.splitlines()[-1])
1 ''
SyntaxError: '(' was never closed
The first line is correct, but it does not run: the error on line 2 is found when the whole input is parsed (r4).
A directory and a zip file as programs
import pathlib
import subprocess
import sys
import tempfile
import zipfile
with tempfile.TemporaryDirectory() as tmp:
app = pathlib.Path(tmp, 'app')
app.mkdir()
(app / '__main__.py').write_text('print("running the app directory")\n')
with zipfile.ZipFile(pathlib.Path(tmp, 'app.zip'), 'w') as z:
z.writestr('__main__.py', 'print("running the zip file")\n')
for target in (app, pathlib.Path(tmp, 'app.zip')):
result = subprocess.run([sys.executable, target], capture_output=True, text=True)
print(result.stdout, end='')
running the app directory
running the zip file
The interpreter runs the __main__.py inside the directory or the archive (r2). This is how a program made of several files can be started with one path.
Interactive mode after a command
import subprocess
import sys
result = subprocess.run([sys.executable, '-i', '-c', 'x = 21'], input='x * 2\n',
capture_output=True, text=True)
print('stdout:', repr(result.stdout))
print('stderr:', repr(result.stderr))
stdout: '42\n'
stderr: '>>> >>> \n'
The -c code sets x, then -i starts interactive mode even though standard input is a pipe (r6). The expression typed at the prompt sees x. The prompts go to standard error, and the result goes to standard output.
Exit statuses
import subprocess
import sys
for code in ['pass', 'raise SystemExit(2)', 'raise SystemExit("bad input")', '1 / 0']:
result = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True)
print(f'{code:30} {result.returncode} {result.stderr.splitlines()[-1:]}')
pass 0 []
raise SystemExit(2) 2 []
raise SystemExit("bad input") 1 ['bad input']
1 / 0 1 ['ZeroDivisionError: division by zero']
A string passed to SystemExit is printed to standard error and gives status 1 (r7). An exception that is not caught also gives status 1, after the traceback.
Pitfalls
- The command
pythonmay not exist, or may start another version (r1). Usepython3, the versioned name such aspython3.15, orpyon Windows, and check withpython3 --version. - A
-ccommand usually contains spaces and quotes. The tutorial says "it is usually advised to quote command in its entirety."1 - One syntax error stops a whole script, including the lines before it (r4). The interactive prompt is different: it runs each statement as it is entered.
Comparison
Python reads and runs source in one step, and the same program also gives an interactive prompt (r2, r5). Go builds first: go run "compiles and runs the named main Go package".4 Python's sys.exit(n) corresponds to Go's os.Exit(n): "Exit causes the current program to exit with the given status code."5 One difference: sys.exit raises an exception, so finally blocks still run (r7), while for os.Exit "The program terminates immediately; deferred functions are not run."5
References
- Using the Python Interpreter, The Python Tutorial 3.15, 2.1. Invoking the Interpreter; 2.1.2. Interactive Mode
- Command line and environment, Python 3.15 documentation, 1.1.1. Interface options; -i
- sys, Python 3.15 documentation, sys.exit
- Command go, Go documentation, Compile and run Go program
- Package os, Go standard library, func Exit
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag X5NG57 Edit this page History Report an error Markdown