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:
https://github.com/pyscripter/pyscripter
Link to the PyScripter installer:
Perfect, thank you! These were my first changes and questions, as always in a new editor!
LikeLiked by 1 person
Thank you very much for getting in touch! Glad it was of some use to you – ed
LikeLike
Thanks for the article. I am currently having an issue with my Pyscripter 2.6, after using it for over a year without any problems. I recently installed pyscripter 3.4.2, but then changed my mind and uninstalled it. After that I couldn’t open my Pyscripter 2.6. So I uninstalled Pyscripter 2.6 and reinstalled it again, then I can open Pyscripter 2.6 again. However, now I can’t delete a line by pressing the backspace/delete button. I find other people having the same issue but there’s no answer yet! Please see this link: https://www.reddit.com/r/Python/comments/7wowa1/help_please_backspace_isnt_working_in_pyscripter/
Do you have any idea on how to solve this problem? Thanks.
LikeLike
Hello! Sorry for the late reply and sorry to hear that you’re having issues.
It looks like you are not the only person having this issue – one or two on the web have had this issue…
I’m not really sure what to suggest apart from the following:
1: Try a “repair” of PyScripter with Programs and Features…..
2: If that fails uninstall PyScripter, and then…
3: Check that the “PyScripter” (and its contents) folder has gone from “C:\Program Files (x86)” directory.. and then…
4: Use REGEDIT to search the registry for any PyScripter reference and delete it (but only if you are comfortable to do this… and you have the right permissions to do so…)
I’ve started to use PyCharm recently as it allows me to use multiple Python instances quite seamlessly (2.7x and 3.5) so it might be worth look at that? There is a free (Community) edition which is easily downloadable and that is what I use.
I hope this helps.
Thanks
ed
LikeLike
Hi Ed,
Thank you for your response.The third step actually works for me. I just deleted all the contents in the “PyScripter” folder and my PyScripter works normally again.
Also thanks for the suggestion about PyCharm. Much appreciated.
LikeLiked by 1 person
Hi Ed, I have this code:
#importar libreria arcpy
import arcpy
# importar capa
archivo = r”H:/Python_ArcGIS_MasterSIG/CAP 12/centros_poblados.shp”
# declarar los campos de trabajo
campo = (“NOM_CCPP” , “SHAPE@XY”)
# construir la sintaxis del searchcursor
cursor = arcpy.da.SearchCursor (archivo , campo)
# realizar un recorrido sobre el searchcursor
for C in cursor:
print C
But this message appear after run:
Traceback (most recent call last):
File “H:\Python_ArcGIS_MasterSIG\codigo\searchcursor.py”, line 11, in
cursor = arcpy.da.SearchCursor (archivo , campo)
RuntimeError: cannot open ‘H:/Python_ArcGIS_MasterSIG/CAP 12/centros_poblados.shp’
I try with python win and in this IDE the code run correctly, why?, very thanks
LikeLike
Hi
So…. Is your shapefile a valid shapefile? Can you see the features in ArcCatalog or do they display in ArcGIS Pro. The issue seems to be with the pathway \ shapefile itself. Just check that the pathway is valid, check that the shapefle name is correct.
The rest if the code looks fine (apart from the lack of indentation for the print statement (and turn it into a function)).
I hope this helps!?
thanks ed
LikeLike