Skip to content Skip to sidebar Skip to footer

How To Print Each Line Of A Script As It Is Run Only For The Top-level Script Being Run?

python's trace module will allow you to run a script printing each line of code as it is run in both the script and all imported modules like so: python -m trace -trace myscript.p

Solution 1:

I stumbled into this problem and found grep to be a quick and dirty solution:

python -m trace --trace my_script.py | grep my_script.py

My script runs in finite time though. This probably won't work well for more complex scripts.

Solution 2:

Maybe lptrace can be useful for you.

Solution 3:

The current accepted answer is good enough if only one file is being used to run all the code but when a whole project of files is being used is insufficient. One way to trace multiple files would be done listing all the files by hand in the following manner:

python -m trace --trace src/main.py | grep "main.py\|file2.py"

For small projects this may suffice but for big projects with a lot of files this may become annoying to list all the necessary file names and can be prone to error. In this manner, ideally you would need to construct a regex for your file names and using find list all the needed files for you to monitor.

Following this idea, I constructed a regex that matches all file names that do not start with a lower dash (to avoid matching on __init__.py files), do not contain any upper case letters in their name nor folder name and have the .py extension. To list all this files in an src folder the match those criteria the command is:

find src/ -type f -regextype sed -regex '\([a-z_]\+/\)\+[a-z][a-z_]\+.py'

Now for using those files in grep we just need to print them with a backward slash and a pipe between them. To print the names in that structure use the following command:

find src/ -type f -regextype sed -regex '\([a-z_]\+/\)\+[a-z][a-z_]\+.py' -printf'%f\\\\|' | awk '{ print substr ($0, 1, length($0)-3)}'

Lastly, we construct the command to match all files with the tracing:

python -m trace --trace src/main.py | grep"`find src/ -type f -regextype sed -regex '\([a-z_]\+/\)\+[a-z][a-z_]\+.py' -printf '%f\\\\|' | awk '{ print substr ($0, 1, length($0)-3)}'`"

This can be used to match filenames in specific folders of the project and the regex can be modified to include other important details that you might have with the names of the files you want to monitor.

Solution 4:

It's not exactly what you want, but you may consider using py.test with "-s" which will prevent py.test from capturing the output of your print statment... So you can put some print statment here and there for each function you have in your script and create a dummy test which just execute your script as usual... You may then see where it failed.

Solution 5:

If you don't get a traceback, you can use the technique called bisection.

Edit your main function or script body and put an exit(1) call aproximately in the middle. This is the first bisection.

Execute your script. If it reaches your exit, you know the fault is in the second half. If not, it's in the first half.

Move the exit to half of the first half, or half of the second half and try again.

With each cycle, you can narrow the location of the fault down by half of the remaining code.

If you've narrowed it down to one of your own functions, bisect that.

Post a Comment for "How To Print Each Line Of A Script As It Is Run Only For The Top-level Script Being Run?"