Python development in Netbeans

NetBeans

NetBeans is a world class platform for creating applications. Originally, it was a Java IDE. With each release, NetBeans is becomming more versatile. The newiest NetBeans 6.5 release has a new module for creating PHP applications. Ruby and C/C++ development modules were added in the past.

In this article, we will talk about a new module for the Python programming language.

Python EA IDE

NetBeans has recently released an Early Access IDE for the Python programming language. Fully functional Python IDE has been an old request or dream of many Python programmers. This NetBeans subproject seems very promising. Currently the project is under development. It has been released as a early access project. It can be downloaded from the following link.

NetBeans community already created an IDE for PHP and Ruby programming languages. Python is the next logical step. This way, the NetBeans platform brings another very popular language to its portfolio.

Netbeans itself is created in Java language and Swing GUI. This combination is ideal for a multiplatform, multilanguage IDE.

First impressions

Installing of NetBeans for Python was very easy. The GTK theme was greatly improved and it looks very well. (This comes from a recent JDK version, not NetBeans itself.) The application is very responsive and fast. On my notebook with 512 MB RAM and 1.6 GB processor, NetBeans performed very well. Without specialized benchmarking tools, there is no way how you can tell it from a C/C++ application. Except perhaps, when you run out of memory and do a lot of swapping.

System requirements for this Python EA module were not specified on the official NetBeans site. These are typical general system requirements for Ubuntu Linux: Minimal system requirements are: 800 MHz processor, 512 MB memory, 650 MB free disk space. Recommended system requirements are: 2.6 GHz processor, 1 GB memory and 850 free disk space.

The Look and Feel

On Linux, the default is the GTK theme. Developers can choose among three lafs. We have the GTK theme, the metal theme and the motif theme. The motif laf looks interesting, but it has several serious design bugs. The GTK theme has vastly improved in the recent versions of JDK.

Metal look and feel
Figure: Metal look and feel
GTK look and feel
Figure: GTK look and feel

The IDE

The following screenshot shows the NetBeans IDE with the traditional metal look and feel.

NetBeans IDE
Figure: NetBeans IDE

The NetBeans IDE is divided into several parts, called simply windows. These are probably the most common windows:

  1. Projects window
  2. Source editor window
  3. Output window

In the projects window, we create and manage our software projects. In the source editor window, we edit our source, metafile, makefile and other editable files. The NetBeans IDE uses the output window for showing various messages. Typically build and run messages.

Features

The following screenshot shows a NetBeans code editor. We can see that comments, keywords and text are all shown in different color. The plus sign also shows the code folding feature.

Code editor
Figure: Code editor

The next screenshot shows a dynamic variable highlighting in action. This is a very handy feature. If you place a cursor on a variable, all occurrences of this variable are marked. The background of the text is highlighted in a different color. It looks good and is unobtrusive. Unlike eg. in Eclipse. This feature is not available in JDeveloper.

Variable highlighting
Figure: Variable highlighting

Creating a project

First, we need to create a Python project. There are several ways, how we can create a Python project. From File menu, we select New Project option, or use the Ctrl+Shift+N shortcut. We can also create the project by using the context menu, right click on the project window and select New Project from the pop up window. The fastest way is to click on the new project button on the edit toolbar.

The edit toolbar
Figure: The edit toolbar

Next we do two steps. Choose a type of a project and then provide a name and location for the project. From the Categories box, we select Python, from the Projects box, Python Project.

In the next dialog window, we provide a name and location for our Python Project. There is also a check box. It is checked by default. It means, that by default a new project is set to be the main project. We can have several projects opened in NetBeans. If we click on the build, debug or run button, this is applied to the current main project in the IDE. We can set, unset a main project from the File menu, or by right clicking on the project icon in the projects window.

The last important thing is to choose the correct version of Python interpreter. There are two options. The classic CPython and Jython. The Jython comes prebuilt with Netbeans. The CPython was located during the installation proces. In our article, we will work with CPython examples.

Python Project
Figure: Python Project

Console Python example

In the first example, we create a simple Python console script.

#!/usr/bin/python

# console.py

print "Netbeans IDE 6.5"
print "for Python language"
print "Early access"

We create a new project as shown previously and type in the code. Execute the script by pressing Shift + F6.

We should see a text printed in the console window.

Console
Figure: Console

Simple PyGTK application

In the next example, we will create a PyGTK application.

Creating a PyGTK application worked out of the box on Linux. No additional settings were necessary. We just need to type in the code and launch the application with the Shift + F6 key combination.

Same for wxPython or PyQt4 applications.

#!/usr/bin/python

# simple.py

import pygtk
pygtk.require('2.0')
import gtk

class Simple:

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)

        win.connect("destroy", self.destroy)

        win.set_title("Simple")
        win.set_position(gtk.WIN_POS_CENTER)
        win.show_all()

Simple()
gtk.main()

This small code example creates a window in PyGTK library. After pressing Shift + F6 a small window will pop up. (The window might be minimized by default).

Simple PyGTK application
Figure: Simple PyGTK application

MySQL database example

NetBeans Python IDE EA comes with a built-in database support. We can create databases, tables or execute SQL commands inside the IDE environment.

In our example, we will create a friends database, friends table and insert some data into the table. A Python script will fetch the data from the table and print them into the console window. We will use MySQL database.

For this example to work, you should have MySQL database system running and have MySQLdb module installed.

To check, if you have MySQL RDMS running, execute the following command:

$ ps -ef | grep mysql
root      5391     1  0 15:35 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
...

The following commands search for and install a Python MySQL database module

$ apt-cache search mysqldb
python-mysqldb - A Python interface to MySQL
python-mysqldb-dbg - A Python interface to MySQL (debug extension)
eikazo - graphical frontend for SANE designed for mass-scanning
sql-editor - editor of SQL databases, with 'join' capability

$ sudo apt-get install python-mysqldb

In the Services window, you can see a database and a web services icon. The services window is whown with Ctrl + 5. The MySQL RDMS was running, when I launched the IDE, so NetBeans recongized it. Otherwise we have to create a new connection by right clicking on the icon.

Services
Figure: Services

The first node in the Databases tree shows a MySQL server instance. The instance has 5 different databases. We create a friends database. Right click on the server instance icon and select Create Database... option. In the dialog window, we specify a name for our database, e.g. friends.

Next we right click on the database friends, and select Connect... option. We specify user name and password. Upon successful connection, we shoud see complete connection icon for the friends database.

Database Connection
Figure: Database Connection

We then right click on the database connection icon and select Execute Command... option. A SQL Commnad window appears, where we will write out SQL commnads.

CREATE TABLE friends
(
  id int not null auto_increment primary key,
  name varchar(40),
  age int
);

We create a friends table. To execute this SQL command, click on the Run SQL icon, or press Ctrl + Shift + E.

INSERT INTO friends (name, age) VALUES
  ("Erika", 21 ),
  ("Robert", 34),
  ("Mike", 22),
  ("Roman", 29),
  ("Lenka", 18)

Here we populate the table with some data.

When we run SELECT * FROM friends SQL code, we get a new window, where we can insert new record, update or delete records.

friends table
Figure: friends table

Finally, we write the Python script.

#!/usr/bin/python

import MySQLdb

try:
    conn = MySQLdb.connect(host="localhost", user="root",
        passwd="", db="friends")
    cursor = conn.cursor()
    cursor.execute("select * from friends")

except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit(1)

else:
    data = cursor.fetchall()
    for id, name, age in data:
        print name, age

    cursor.close()
    conn.close()

If all went OK, we get the following list of friends in the output window.

Erika 21
Robert 34
Mike 22
Roman 29
Lenka 18

Issues

I have encountered several issues when working with Python IDE. The IDE is in beta state and it is not finished yet. I will mention some of them.

For example, there are issues with the code folding. Code completion does not work well. Too many options pop up, so the programmer is lost in all those values. No matter what I do, I can't get rid of the start page. It is still there, when you restart NetBeans. When you resize the window to a small size and bring it back to original size, toolbars are scattered. The about dialog is huge, but says nothing about the fact, that it is a Python IDE. The Ctrl + / keys comment the code, but this key combination is not available in any of the menus. etc.

Final word

The NetBeans IDE for the Python language looks promising. There are plans for Django and TurgoGear web frameworks. Python programmers can look forward to a finally good IDE for their programming language.