Published 7/2/2024
Debugging Python in VSCode
Using the code.interact() function to launch an interactive code interpreter
They say you learn something new every day, but I was shocked to learn about a fantastic Python feature I had no idea about.
Apparently, you can pause execution of a Python file and open up an interactive terminal with the local variables! I discovered this while watching Andrej Karpathy's video about reproducing GPT-2 (trust Karpathy to know about random tricks like this.)
Just import code
at the top of your file, then put code.interact(local=locals())
at any place in your code. Voila! When execution reaches that point, a Python interpreter will open in the terminal with access to all local variables. You can press CTRL+D
to close the interpreter and continue execution, or type quit()
to quit the terminal and stop execution.