Sign in to change your settings
Sign in to your Python Morsels account to save your screencast settings.
Don't have an account yet? Sign up here.
Let's interactively inspect Python modules.
A non-functioning Python script
Here we have a program (add.py) that's meant to add numbers together:
import sys
numbers = sys.argv[1:]
print(sum(numbers))
When we run this program, we'll see an error:
~ $ python3 add.py 4 7
Traceback (most recent call last):
File "/home/trey/add.py", line 4, in <module>
print(sum(numbers))
^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'
We could try to debug this program using Python's breakpoint function.
Interactively inspecting a Python script
But let's instead play with Python's -i flag.
If we pass the -i flag to Python, Python will run our module as usual, but once we get to the end of our module, our program doesn't exit!
Instead, we're dropped into a Python REPL:
~ $ python3 -i add.py 4 7
Traceback (most recent call last):
File "/home/trey/add.py", line 4, in <module>
print(sum(numbers))
^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
The -i flag allows us to interactively inspect a Python module, meaning Python runs the module and then drops us into a REPL.
This is handy because now we can poke around and see what variables were made:
>>> __name__
'__main__'
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package_
_', '__spec__', 'numbers', 'sys']
Let's take a look at our command-line arguments:
>>> sys.argv
['add.py', '4', '7']
We can see that 4 and 7 were passed in as command-line arguments.
Now let's take a look at our numbers list:
>>> numbers
['4', '7']
Ah!
Our numbers list is actually a list of strings, because we never converted the arguments coming into our program from strings to numbers.
That's the bug in our code.
Interactively inspecting a Python module
We just used the -i flag to inspect a Python script.
But we can also use -i to inspect a Python module that isn't meant to be used as a script.
Here's a fibonacci module:
from math import sqrt
root5 = sqrt(5)
phi = (1 + root5)/2 # The golden ratio
def nth_fibonacci(n):
"""Return the n-th Fibonacci number."""
return round(phi**n / root5)
This fibonacci module is meant to be imported, not run from the command line.
But if we use the -i flag to run this module, it's pretty much the same as if we had copy-pasted the entire contents of that module into the Python REPL:
$ python3 -i fibonacci.py
>>>
Meaning, we now have access to all of the names that were defined in that module.
We can call the nth_fibonacci function, and we can access phi:
>>> nth_fibonacci(10)
55
>>> phi
1.618033988749895
This is similar to starting the Python REPL and then doing a star import on your module:
>>> from fibonacci import *
>>> nth_fibonacci(10)
55
Use -i to open a REPL inside your module
Python's -i flag can be used to inspect a Python script or module interactively, meaning we can poke around with the current state of the Python module right after it's been run.
The -i flag is not meant to replace the Python debugger, but it can be pretty handy sometimes.
A Python tip every week
Need to fill-in gaps in your Python skills?
Sign up for my Python newsletter where I share one of my favorite Python tips every week.
Python's assert statement can be used in automated tests, and to validate certain conditions that might cause bugs if left unchecked.