1.5 More about Python installation (Windows)

Displaying error messages correctly

On Windows systems, if your script does not end with a raw_input() or show() statement which pauses the execution at the end, the console window closes preventing to read the displayed outputs. Even worse, if there is a bug in the Python script, the Python console closes immediately so that you have no chance to read the error messages.

This annoying feature of Python is easy to correct. Just open an editor (as Notepad), copy-paste the following code and save the file under the name “python.bat”:

@C:\Python27\python.exe %*
@PAUSE

Then:

  • put the file in the Python folder (generally, this folder is at the top-level, such as “C:\Python27”)
  • associate all .py files with this new “Python.bat” executable. With Windows XP, you do this by opening an explorer window, going to the menu “Tools” then “Folder Options”, selecting the tab “File Types”, choosing the .py extension and clicking on the button “Change” in front of the text “Open with” to select “Python.bat”.

Once this is done, whenever you double-click on a file with the .py extension, it is opened with the “Python.bat” file. This new executable redirects to “Python.exe” but also makes sure that all error messages are displayed.

If you want to pause the script only if an error occurs during execution you can use the following, but be careful that it may not work on some systems, where the returned error code is still be 0 (=successful execution) even with a bug crashing the script:

@C:\Python27\python.exe %*
@IF %ERRORLEVEL% NEQ 0 PAUSE

Installing (and configuring) an editor for Python

Python has no dedicated program to edit the scripts. There are many IDE (Integrated Development Environment) softwares floating around allowing you to edit and execute, sometimes even debug your Python scripts. You can check there and choose your preferred program. I tested a few of them, and the software I would recommand is Notepad++. Notepad++ is a freeware editor, that is light, robust and easy to use. It can be downloaded and installed from here.

Once Notepad++ is installed, you may want to configure it to run your Python scripts FROM the editor, with a shortcut as for instance Ctrl+E. To do it, open Notepad++. Go into the “Run” Menu, choose “Run... F5” and copy-paste the following lines:

C:\Python27\python.bat "$(FULL_CURRENT_PATH)"

Then click on “Save” and specify the shortcut of your choice. The instruction that is entered allows you to pass the name of the file that is currently opened in Notepad++ (through the string “$FULL_CURRENT_PATH”) to python.

That’s all ! You can now write a simple script test.py with Notepad++, and execute it by pressing Ctrl+E (or any other shortcut you specified)...

_images/notepad++.jpeg

Table Of Contents

Previous topic

1.4 64-bits systems

Next topic

1.6 First steps with Python

This Page