Working with script templates in PyScripter

In a previous post I went over some PyScripter configuration which the attendees to the “Introduction to Geoprocessing Scripts using Python” course carry out. These things include adding line numbers to a script and making sure full code completion was available for the ArcPy site package.

This article will explain how to create script templates – scripts with ready to go code. This is another nice feature of PyScripter.

Script templates can be used to provide a new script with code already stubbed out such as:

  • Add frequent comments.
  • Import commonly used modules / site packages.
  • Provide a structure for runtime error handling and log file generation.

An example of a script template is shown below:

Default Script Template

This default code appears when you press the New Python Module button NewButton or if you go to the File menu and then choose New > New Python Module.

So how do you change this code template?

By default you are provided with the following code (plus comments):


This can be easily changed by:

1: Select from the Tools menu > Options > File Templates menu item which will display the File Templates dialog box.

This dialog contains a number of different types of templates which you can use in case if you ever need to create an XML or HTML document within PyScripter.

2: Under Name, choose Python Script.

3: In the File Template section locate the Template area.


This is where you can add your template code which will appear when you create a new script module.

You might want to include the following:

  • Comments
  • import statements
  • Error handling and logging

**NB** Please be aware that when you update this template there is no way of regaining the original code so it might be a good idea to copy the code into a file just in case….

For example:

# Name:        $[ActiveDoc-Name]
# Purpose:
# Author:      $[UserName]
# Created:     $[DateTime-'DD/MM/YYYY'-DateFormat]
import arcpy, os, sys
    # Add implementation here
    arcpy.env.workspace = ""
except arcpy.ExecuteError():
    # Capture GP error
    print (arcpy.GetMessages(2))
except Exception as e:
    # Capture general Python error
    print (e)

Notice that in the comments part of the template code there are a number of $[variable] entries, for example $[UserName]. You can probably guess that these custom parameters are similar to inline variables – you can find more information about these parameters via Help > PyScripter > Parameters help documentation.

4: Press the Update UpdateButton button to save your updated code back to the template.

5: Press the OK button


When you press the New NewButton button your new module will have your updated template code.

Can I create multiple script templates?

Yes! You can choose from a list of code templates which you have created.

So “How do I create these other templates?

Creating a new script template is pretty easy. It is all achieved, once again, via the File Template dialog – the same dialog which you used a moment ago to change the default code template.


1: From the Tools menu select Options > File Templates menu item.

This will display the File Templates dialog box.

2: Click on Python Script in the Name section to display the template code for a new Python module.


This is the code which is displayed when you create a new module via the New NewButton button or File menu > New > New Python Module menu item.

3: In the Name text box type in a new name for your new template, for example GPScriptTemplate.

Note that your name can not have any spaces within it.

4: Press the Add AddButton button

This will add your new template to the bottom of the template list.

5: Add your template code by amending what is already there or by writing your own code.

6: Once you have added your code press the Update UpdateButton button to save your changes to the template.

7: Press the Up UpButton button to move your template up the template list so all the all the Python scripts are grouped together.

8: Press the OK button to commit your changes and close the dialog.


To choose from a list of code templates you have created you should:

1: Go to the File menu choose New > New File. This will display the New File dialog box.

2: Under Categories, choose Python and you will see your named template, for example GPScriptTemplate


3: Press the Create button and your code contained in the template will be added to your newly created script module.


So, if you use PyScripter, this could be Heaven for Everyone – it’s worth giving them a go. Script templates can make writing your code just that little bit quicker, especially if you tend to write the same code over and over again.


Configuring PyScripter for writing Python scripts

PyScripter is a great Interactive Development Environment (IDE) for creating Python scripts as it provides an excellent environment for writing and debugging your code. This is the IDE which attendees to the “Introduction to Geoprocessing Scripts using Python” course use.


There are a couple of things which I get the class to do to configure PyScripter as these additional steps enhance the script writing experience within the course. This blog will lead you through these additional steps.

If you want to use PyScripter then you can access the source files from this location on GitHub, but if you do not have the time or the energy to compile the files then you can download the PyScripter installer from here. It is very easy to do and will take you no more than two minutes to complete. Links to the source files and the installer are provided at the end of this article.

Add Line Numbers

Line numbers should be made visible within your Python script if for no other reason than to aid the debugging process.

When a run-time error is generated a Traceback error is created. Amongst other things it lets you know which module your error occurred in, the type of error and the actual line of code which generated the error. Also included is the line number. I think you’ll agree it is easier searching for the line number when it is displayed than searching for a particular line of code.

To add line numbers to your module in PyScripter you should:

  • From the Tools menu choose Options > Editor Options
  • On the Display tab locate the Gutter section and tick the check box next to Show line number


  • Press the OK button to add line numbers to the left hand side of your script.


Remove the script border

if you use PyScripter then you may have noticed that on the right hand side of your script is a vertical line. This is used as a guide to suggest the maximum length for a line of code. This is easier said than done, especially when writing scripts which involve long workspace pathways or if you like adding comments at the end of a line of code.


So how can it be removed?

In the example above I could remove the hard-coded pathway and use arcpy.GetParamerterAsText() to pass the workspace value into the script.

I could also remove the line by quickly changing an option in PyScripter.

To do this you should:

  • From the Tools menu choose Options > Editor Options
  • On the Display tab locate the Right Edge
  • Whatever the value is for Edge Column change it to “0”.


  • Press the OK button and the border edge is no longer visible.

Initiating code completion for ArcPy

You may find that the intelligent code completion (AKA IntelliSense) for the arcpy site package is not always complete. In the example below notice that when arcpy.env is typed the overwriteOutput property is available, but very little else is presented in relation to the members of the env class.


It is fairly easy to ensure that you have full code completion for the arcpy site package, or for any third party / user defined module:

  • From the Tools menu, select Options > IDE Options
  • In the Code Completion region, of the IDE Options dialog, locate the Special packages
  • After the scipy entry add a comma and type arcpy as shown below:


  • Press the OK button to dismiss the dialog box and now when you retype env. you will now see the full code completion:


In a future post I will have a look at creating “script templates” which are handy if you find that you are writing the same “boiler-plate” code within your newly created scripts – but I will leave that Bijou for another time

Are there any other tips you have come across? If so then let everyone know about it by replying to blog and I will post your comments up!

Link to source files on GitHub:

Link to the PyScripter installer: