Using Eclipse to develop GRASS Python programs

From GRASS-Wiki
Revision as of 05:43, 13 May 2009 by ⚠️HamishBowman (talk | contribs) (fancy arrows)
Jump to navigation Jump to search

How to use Eclipse to develop Python scripts that access GRASS object

Recent releases of GRASS have emphasised the use of Python as the language of choice for developing scripts which access GRASS objects and can be used to automate operations without GRASS running.

This short tutorial shows how to set up a complete development environment using the Eclipse IDE to develop, debug and run Python programs that access GRASS objects. These instructions apply to both Windows and Linux and possibly Mac as well since all the packages required are open source software which runs on all three platforms.

Please note that this is not an Eclipse, nor a Python, nor a GRASS tutorial. It is just a description on how to get all the above to work together. It assumes some (but not much, because I don't have much!) knowledge of all the above.

Prerequisites (Release versions are as at the time of writing)

  • GRASS 6.4 and Python 2.6.x
  • Eclipse 3.4.x
  • Pydev 1.4.5 Eclipse plug-in for Python
  • Optionally, other plugins for Eclipse

GRASS 6.4.0RC4 and Python 2.6.x

Many Linux distributions already include GRASS in their packaging but you must ensure you download the latest (GRASS 6.4.0RC4) release for Linux, MS-Windows, and Mac, available in binary (executable) from the GRASS download site, and install it as instructed for your platform. You will also need to download Python 2.6 for your platform from the Python.org site and install it as required for your platform making a note of where the python executable (python.exe in MS-Windows) is installed. Most recent Linux already come with Python 2.6.2 installed.

Eclipse

Eclipse 3.4.2 is the minimum version recommended, but other releases could work as well. Download it from the Eclipse download page. The Ganymede 3.4.2 release for Java developers is fine (85MB). Installing Eclipse is very easy because installations consists simply of unpacking the download package in a directory of your choice. To avoid permission problems in Linux, install Eclipse in your Home directory or a subdirectory.

Pydev

Pydev is an Eclipse plugin that adapts the Eclipse platform to the specific needs of Python program development, debugging and testing. It is not the only Python plugin available, but the one largely recommended. The current release is 1.4.5 and to install it you should use the Eclipse Update Manager mechanism, as detailed below.

Open the Eclipse platform and click on the Help → Software Updates menu entry, click on the Available Software tab and click on the Add Site button. This will open a dialog box where you insert the following URL: http://pydev.sourceforge.net/updates/ . then press OK and follow the instructions. This will result in the Pydev plug-in being automatically downloaded and installed among your other Eclipse plug-ins and furthermore it will automatically inform you in future when new updates are available. The Home page for Pydev is: http://pydev.sourceforge.net/ . At this point you have all the software pre-requisites installed, ready for the next step.


Creating a new project

When you first open the Eclipse platform after a new installation you will be asked to set-up a workspace, which will normally be in your home directory by default, then you will see a Welcome screen with various buttons. Click on the one that opens the Eclipse workspace, which will be empty and probably be Java oriented. To change it to be Python oriented you must open what in Eclipse is called a "perspective", which is a combination of views and windows which suits the plug-in you are actually using at any one time. So, go to the Window → Open Perspective sub-menu and select Pydev. This will open the Pydev perspective where most of your development work will be done, and since new development starts with a new Project, now go to the File → New menu and select Pydev Project, and follow the prompts to create a new empty Python project which we will name Myproject. Finally create the following new Python sourc