Python Development In Eclipse

As your code gets bigger, effective code management becomes an integral part of your project. This post is about setting up Python development environment in Eclipse. Most of the Python developers will already be aware of this. For those of you who are not, read on. Eclipse is an IDE which supports many different languages. It has plugins for various different frameworks as well. We will discuss about how we can set up everything from scratch. Once you set everything up, you will be able to run and debug you Python code easily. It provides many different options and it has a nice UI too.  

Downloading and Installing Eclipse

If you already have Eclipse, then skip to the next section. The following steps outline the procedure to download and install Eclipse IDE on your machine.

  1. The latest version of Eclipse is 4.2 (Juno). Download Eclipse IDE for Java Developers from this website.
  2. Unzip the downloaded file to a convenient location on your machine.
  3. Open eclipse.exe found in the folder where you unzipped the contents.
  4. You will be asked to choose a default workspace here. Pick a folder that is easy to remember, for example:
    Windows: C:\Users\yourname\Eclipse\workspace
    Linux: /home/yourname/Eclipse/workspace 
    Mac: /Users/yourname/Eclipse/workspace
  5. Verify that Eclipse starts properly. You should get a welcome screen if you open it.

Installing PyDev

PyDev is an Eclipse plugin that is used to put in Python functionality into Eclipse. The functionality of Eclipse IDE can be extended with additional software components. These components are called plugins. The idea behind the Eclipse platform is to provide a common environment that can be modified and customized by creating plugins. Plugins are created to do some specific tasks that a work group might need while developing a software product. The following steps outline the procedure to install PyDev:

  1. In Eclipse, go to Help -> Install New Software…. A new window should pop up.
  2. Click the Add… button at the top right of the window.
  3. In the Name field, put PyDev and in the Location field, put in http://pydev.org/updates. Click OK. It will go back to the previous window and populate the list.
  4. Check the PyDev option. The second one in the list is optional. Click Finish.
  5. It will start installing now. If it asks if it should install Aptana, just check the box and continue.

Setting up Python interpreter

Now we have installed the Python plugin for Eclipse, but Eclipse doesn’t know where Python is installed on your machine. So we need to configure the Python path before proceeding further. If you don’t know where Python is installed on your machine, just open the terminal and type the following:

$ which python

It will display the path to your Python executable. If you want to know the Python version, type the following:

$ python --version

The following steps outline the procedure to configure the Python interpreter in Eclipse:

  1. In Eclipse, go to Eclipse -> Preferences (this is for Mac). On Windows, it would be Window -> Preferences. A new window should pop up.
  2. In the new window, you should see PyDev listed in the left pane. Expand it and select Interpreter – Python.
  3. Select New… button near the top right of this window. A small new window should pop up.
  4. For the Interpreter Name, choose Python2.7 (or whatever version of Python you have on your machine). For the Interpreter Executable, choose the path which was displayed earlier when you used the “which python” command on the terminal. Click OK to continue. It will go back to previous window.
  5. Click Apply and OK buttons one after the other located near the bottom right of this window.

Now we are all set to work with Python in Eclipse.

Creating a new Python project in Eclipse

Now that we have everything set up, let’s test it out. Go to File -> New -> Other. Expand PyDev and select PyDev Project. Give a meaningful project name in the Project Name field. Near the bottom, select the second option which says “Create ‘src’ folder and add it to the PYTHONPATH?”. Click Finish. It will go back to the main Eclipse window.

In the Package Explorer (left pane in this window), you should see your project listed. Right click on the “src” folder and select New -> File. For the filename near the bottom, say “filename.py” (you can use any other name but use the .py extension). In the Package Explorer, expand “src” and double click on “filename.py”. It will open the file. To test if everything is working properly, type the following and select Run -> Run: 

print "Hello World"

You should be able to see Hello World printed on the console (bottom of the window).

Debugging

In order to debug Python code in Eclipse, select Run -> Debug. It will ask if you want to open Debug perspective. Say yes. It will open a new perspective and you will get a whole list of options. You can go back to the PyDev perspective by selecting that option near the top right (you will see Java, DDMS, Pydev and Debug options listed over there in the form of buttons). In the Debug perspective, you can insert breakpoints anywhere in the code and go step by step, analyze your code, monitor variables and so on. There are many different things you can do with a debugger. The concept of debugging remains the same, you should just use whatever features you need for your project. You can check out this very useful tutorial. It describes all the options available in Eclipse debugger and shows how to use them as well.

———————————————————————————————————–

2 thoughts on “Python Development In Eclipse

  1. things hard to build, difficult to replace, and costly to substitute.
    Department of Agriculture predicts that overall food inflation next year, about 2% to 3%.
    What do you think when a new potential supplier approaches you with the
    selling phrase, “I’m as good as the one you use now”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s