How to run script from interpreter?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 17 09:47:30 EST 2018


On Fri, 16 Feb 2018 14:18:16 -0800, windhorn wrote about running scripts 
from the interpreter:


[...]
> That works, but nothing is very convenient for debugging simple scripts.
> If I run the script from a command prompt it works, but I lose all my
> other stuff (debugging functions, variables, etc.).

I'm sorry, reading your description of the various steps you tried 
brought to my mind a chef insisting that the only cooking implement he 
needs is a spoon, and then complaining that it's not well suited to 
slicing meat :-)

The interactive interpreter, or REPL (Read-Eval-Print Loop) is not 
designed for the edit-run-debug-edit cycle. But it can be used as a 
single component of that development process.

For me, the tool I use is a set of re-usable tools:

- a text editor;
- a system command prompt in a terminal/console window;
- a Python REPL for running code snippets and looking up help(obj).

Other people prefer a IDE like Spyder (or many others). Either way, 
they're designed for the edit-run-debug-edit cycle.

I edit the source code of my script in the text editor, then run it from 
the command prompt using

    python myscript.py

If I need the debugger, I use:

    python -i myscript.py

I often use test driven development, so I have a set of tests written. I 
edit the tests, then run the test suite:

    python myscript_tests.py

then edit the script to make the tests work.

The best part of this is each time you run the script, it is guaranteed 
to be running *fresh*, with no left-over cruft from previous runs.

Trust me, after you've spent hours trying to debug a mysterious problem 
in your code, only to discover that it was a side-effect of re-using the 
same interactive session that ran an older version of your code, you too 
will shudder at the thought of trying to do all your development in an on-
going, persistent REPL session.


> More a comment than a question but seems like sometimes execfile() is
> the right tool.

I'm sure that it is; but the question, the right tool for what?



-- 
Steve




More information about the Python-list mailing list