What Does -i Mean In Python

5 min read

In Python, the -i flag is a command‑line option that tells the interpreter to drop into an interactive session after executing a script. This seemingly small switch can be a powerful aid for debugging, exploration, and learning, especially when you want to inspect variables, call functions, or test snippets without rewriting code in a separate REPL session. Below is a detailed look at what -i does, how it works, when it is useful, and what you should watch out for Simple, but easy to overlook..


What Does -i Mean in Python?

When you launch Python from a terminal, you can pass various options that modify the interpreter’s behavior. The -i option stands for “interactive after script execution.”

  • Normal execution: python script.py runs the script and exits immediately when the script finishes or raises an unhandled exception.
  • With -i: python -i script.py runs the script first, then, instead of terminating, it launches the standard Python interactive interpreter (the REPL) with the script’s global namespace already loaded.

Put another way, after the script’s top‑level code has run, you are left at a >>> prompt where you can inspect any variable, function, or class that the script defined.


How the -i Flag Works Under the Hood

  1. Argument parsing: The CPython launcher scans sys.argv for flags. When it sees -i, it sets an internal flag Py_InteractiveFlag to true.
  2. Script execution: The interpreter executes the supplied script file exactly as it would without -i.
  3. Post‑script hook: If Py_InteractiveFlag is true and the script terminates normally (or via SystemExit with code 0), the launcher calls PyRun_InteractiveLoop, which starts the REPL.
  4. Namespace preservation: All objects created during script execution remain in the __main__ module’s namespace, so they are accessible from the interactive prompt.

This mechanism is identical to what happens when you start Python with no arguments and then manually exec(open('script.py').read()), except that -i does it automatically and preserves the exact __main__ module state Simple, but easy to overlook..


Practical Examples

Basic Usage

$ python -i hello.py
Hello, world!
>>> 

If hello.py contains:

print("Hello, world!")
name = "Alice"
def greet():
    return f"Hi, {name}!"

After the script runs, you are at the >>> prompt and can type:

>>> name
'Alice'
>>> greet()
'Hi, Alice!'

Debugging a Failing Script

Suppose calc.py raises an exception:

# calc.py
def divide(a, b):
    return a / b

result = divide(10, 0)
print(result)

Running it normally stops at the traceback:

$ python calc.py
Traceback (most recent call last):
  File "calc.py", line 5, in 
    result = divide(10, 0)
ZeroDivisionError: division by zero

With -i, you get the traceback and an interactive session:

$ python -i calc.py
Traceback (most recent call last):
  File "calc.py", line 5, in 
    result = divide(10, 0)
ZeroDivisionError: division by zero
>>> 

Now you can examine the state:

>>> divide.__doc__
'Return a divided by b.'
>>> divide(10, 2)
5.0
>>> b = 0
>>> a = 10
>>> a / b   # reproduces the error
Traceback (most recent call last):
  File "", line 1, in 
ZeroDivisionError: division by zero

You can even redefine the function on the fly to test a fix:

>>> def divide(a, b):
...     if b == 0:
...         return float('inf')
...     return a / b
...
>>> divide(10, 0)
inf

Exploring Modules

If you import a module inside the script, its objects also become available:

# demo.py
import math
radius = 5
area = math.pi * radius ** 2
$ python -i demo.py
>>> radius
5
>>> area
78.53981633974483
>>> math.e
2.718281828459045

When to Use -i

Situation Why -i Helps
Post‑mortem debugging Inspect variables after an exception without adding pdb breakpoints.
Interactive learning Run a tutorial script and then experiment with the introduced concepts. Which means
Rapid prototyping Execute a setup script that defines helper functions, then test them live.
Teaching / demos Show students the script’s output, then let them play with the resulting state.
Checking side effects Verify that a script correctly modified files, environment variables, or global state.

In contrast, if you only need to run a script and exit, omit -i. If you want an interactive session without running any script first, simply start python or python -c "" (or use ipython, bpython, etc.).


Differences Between -i and Similar Flags

Flag Meaning Typical Use
-c <command> Execute the supplied Python command string and exit. Worth adding: Running library tools (python -m venv myenv). Also,
-B Don’t write . pyc files. On the flip side, version)"`.
-m <module> Locate and run a module as a script (python -m module_name). In real terms, Production builds where speed matters.
-O / -OO Optimize bytecode (remove assertions, docstrings).
-i Run script, then enter interactive mode. Debugging, exploration, teaching.

Note that -i can be combined with -c or -m. For example:

python -i -c "import math; x = math.sqrt(2)"

will evaluate the expression, then drop you into a REPL where x and math are already defined.


Benefits of Using -i

  1. Immediate feedback loop – You see the script’s output, then can test variations instantly.
  2. No extra boilerplate – You don’t need to add if __name__ == '__main__': blocks or manual exec statements.
  3. Preserves exact execution context – All imports, global variables, and
Currently Live

Just Went Online

Similar Vibes

On a Similar Note

Thank you for reading about What Does -i Mean In Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home