Question : Best Way to Test a Python Script?

Hello,
I am new to Python scripting and want to know the best way to check a script to see what, if anything, is wrong with it.  Sometimes when I execute a script (e.g. script.py) by just double clicking on it - the cmd window pops up and will shut with no text and other times, it will have text but close before I can read it.  I would just like to know the best and/or easy way to check my script for any problems.

Thank you,
MJ

Answer : Best Way to Test a Python Script?

The easy way is to use more windows when developing the script. The first window will be the one of the editor, the second window will be cmd window launched manually. You can create a shortcut to cmd with working directory pointed to the directory where the tested script is located. Then (in the cmd window) you can simply type the name of the script. The result is that the cmd will not be closed.

The alternative is to set properties of the cmd so that the window (created automatically as a result of double clicking on script.py) does not close when finished. However, you probably does not want that behaviour for other applications that open cmd.

Another possibility is to create a batch file where you launch your Python script and put the pause command after that.

Sometimes overusage of try/except construct during the development may mask simple syntax errors. Postpone usage of try/except to the later development phase. This way, all exceptions will stop the execution of the tested script and the reasons will be displayed in the cmd window.
Random Solutions  
 
programming4us programming4us