Developing a Python Application Using NetBeans
Written by James Branam
This document provides an introduction to programming with the Python programming language in NetBeans IDE. Python is a very popular general-purpose high-level programming language with a design philosophy that emphasizes programmer productivity and code readability. This tutorial demonstrates how features of the NetBeans IDE can best benefit the Python programmer. Each section of the tutorial demonstrates usages of Python and identifies in the IDE features that can be used with Python programming. This tutorial is not intended to teach the Python programming language, but rather demonstrate how the NetBeans IDE can be used with Python-based applications.
You need the Python EA download of the NetBeans IDE or the Python plugins installed to complete this tutorial.
Expected duration: 30 minutes
Contents
To follow this tutorial, you need the following software and resources.
Overview of the Application
In this tutorial, you create a temperature converter step-by-step, adding functionality with each step. The first version of the application displays a list of Celsius temperatures converted to Fahrenheit, in increments of ten. In the second version of the application, the user is prompted for input, and the output is based on that input. The third version of the application features input validation. Incorrect input results in an error message, prompting the user for new input.
Creating the Project
Begin by creating a new Python project in the IDE. The New Project wizards contains built-in templates for numerous project types. You can access the New Project wizard in a number of ways. You can choose File > New Project in the main toolbar, or you can right-click in the Projects window and choose New project.
-
In the main toolbar, choose File > New Project.
The New Project wizard opens with Python displayed as the category.
Note: Python is displayed as the project when only the Python EA version of the IDE has been installed on your machine. Other categories may appear if Python EA was added to the IDE as a plugin.
-
Select Python Project as the Project Type, as seen in the following image. Click Next.

-
Enter TemperatureConverter as the Project Name, as seen in the following image. Leave the Set as Main Project checkbox and Create Main File checkbox selected.
The IDE creates a project folder based on the name of the project. You can change the name and location of this folder.
-
Select the Python Platform you want to use from the drop down list, then click Finish to create the project.
The IDE creates the project. When you create the project, the IDE does the following for you:
- Adds the TemperatureConverter project node in the Projects window
- Creates a TemperatureConverter.py file in the Projects window.
- Displays the project's file hierachy in the Files window.
- Opens TemperatureConverter.py in the Source Editor
Note how your project is displayed in the Projects window.
When you click the Files tab, note how the File window displays the project hierarchy. The following image shows the Files window with all of the project's nodes expanded.
Note that the TemperatureConverter.py file is open in the Source Editor of the IDE, displaying basic information, as seen in the following image.
Adding Python Code
In this section, you add Python code to your new project. This code is a basic Python application that converts temperatures from Celsius to Fahrenheit, in increments of ten.
-
In the Source Editor, erase the existing code under the __date__ string.
Note: You can also choose to delete all of the code
The Source Editor should then resemble the following image.

-
Type or paste the following code in the Source Editor.
def temp():
print "Welcome to the NetBeans Temperature Converter." print "Celsius Fahrenheit" # This is the table header. for celsius in range(0,101,10): # Range of temperatures from 0-101 in increments of 10 fahrenheit = (9.0/5.0) * celsius +32 # The conversion print celsius, " ", fahrenheit # a table row
temp() |
-
The Source Editor should resemble the following image. Note the color-coding of elements displayed in the editor.

-
Now, in the Source Editor, place the cursor in the temp function definition, right-click, and choose Rename, as seen in the following image.

-
In the Rename Method dialog box, type converter as the new name of the method, as seen in the following image.

-
Click Preview.
The IDE displays two versions of the source file in the Refactoring window: the version before the change and the version after the planned refactoring. Note that you can choose the scope of the refactoring by means of checkboxes in the left panel of the Refactoring window.
-
If you are satisfied with the outcome of the refactoring, click Do Refactoring. If you are not satisfied with the outcome, click Cancel.
Note how the refactoring has renamed every instance of temp to converter.

Running the Project - 1
Now it is time to run your project. In NetBeans IDE, there are several ways to run your project. You can click the run icon in the main toolbar, or you can right-click the project node in the Projects window and choose Run.
-
In the Projects window, right-click the project node and choose Run.

The results are displayed in the Output window at the bottom of the IDE, as seen in the following image.
Adding Input
Now you create user input for your application. In the previous section, the IDE converted ten prespecified temperatures from Celsius to Fahrenheit. This section of the tutorial describes how you can add user input to the application, in which the user is prompted to choose the direction of conversion and how many times the conversion is performed.
-
Open the Source Editor and remove the source code that is below the first print statement.
-
Rename the convertor function to temp. The result should resemble the following code sample.
def temp():
print "Welcome to the NetBeans Temperature Converter." |
-
Type or paste the following code in the Source Editor, under the first print statement.
y=1 n=0 z=input("Press 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit, or 3 to quit:") if z!=1 and z!=2 and z!=3: e=input("Invalid input, try again?(y or n)") if e==1: t='' temp() if e==0: t="Thank you for using the NetBeans Temperature Converter."
|
This code adds the initial user input, specifying the intent to convert. Note how a boolean variable is used to indicate whether the user wants to continue.
-
Right-click anywhere in the Source Editor and choose Format, as seen in the following image.

-
Next you add code expressing the direction of conversion. The application performs the conversion based on the numerical input. Type or paste the following code in the Source editor, directly below the code in step 3.
if z==1: # The user wishes to convert a temperature from Fahrenheit to Celsius x=input("Input temperature in Fahrenheit:") t=(x-32)/1.8 print "The temperature in Celsius is:" if z==2: # The user wishes to convert a temperature from Celsius to Fahrenheit x=input("Input temperature in Celsius:") t=(x*1.8)+32 print "The temperature in Fahrenheit is:" if z==3: # The user wishes to quit the application t="Thank you for using the NetBeans Temperature Converter." print t |
-
Now you add the last of the source code, asking the user if another conversion is requested, or whether to end the application. Once again, boolean variables are used. Type or paste the following code in the Source editor, directly below the code in step 4.
if z==1 or z==2: a=input("Do you want to perform another conversion?(y or n)") if a==0: t="Thank you for using the NetBeans Temperature Converter." if a==1: t= '' temp() print t |
-
If you decide to run the application now, nothing is displayed in the Output window. The reason for this is that you have not closed the temp method. When you starting typing the method at the end of the source code, click Crtl+Space to enter code completion mode. The IDE suggests code for you, as seen in the following image. Use the arrow keys to navigate among choices. Press Enter to accept the selection.

-
After typing or pasting all the code for your application in the Source Editor, the code should resemble the following code sample. If the code does not resemble the following code sample, make all necessary corrections before running the application.
def temp(): print "Welcome to the NetBeans Temperature Converter." y=1 n=0 z=input("Press 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit, or 3 to quit:") if z!=1 and z!=2 and z!=3: e=input("Invalid input, try again?(y or n)") if e==1: t='' temp() if e==0: t="Thank you for using the NetBeans Temperature Converter." if z==1: # The user wishes to convert a temperature from Fahrenheit to Celsius x=input("Input temperature in Fahrenheit:") t=(x-32)/1.8 print "The temperature in Celsius is:" if z==2: # The user wishes to convert a temperature from Celsius to Fahrenheit x=input("Input temperature in Celsius:") t=(x*1.8)+32 print "The temperature in Fahrenheit is:" if z==3: # The user wishes to quit the application t="Thank you for using the NetBeans Temperature Converter." print t if z==1 or z==2: a=input("Do you want to perform another conversion?(y or n)") if a==0: t="Thank you for using the NetBeans Temperature Converter." if a==1: t= '' temp() print t temp() |
Running the Project - 2
Now it is time again to run your project. In NetBeans IDE, there are several ways to run your project. You can click the run icon in the main toolbar, or you can right-click the project node in the Projects window and choose Run.
-
In the Projects window, right-click the project node and choose Run.

The results are displayed in the Output window at the bottom of the IDE, as seen in the following image.
Adding Validation
Now you add validation to your application. In the previous section of this tutorial, you added user input to our application, in which the user is prompted to choose the direction of conversion and how many times the conversion is performed. Now we add validation to check the validity of user input.
- Open the Source Editor of the IDE.
Replace the contents of the temp method with the following source code in bold.
def temp(): print "Welcome to the NetBeans Temperature Converter." z=raw_input("Press 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit, or 3 to quit:")
a==no_input if z=='1': # The user wishes to convert a temperature from Fahrenheit to Celsius x=input("Input temperature in Fahrenheit:") t=(x-32)/1.8 print "The temperature in Celsius is:" elif z=='2': # The user wishes to convert a temperature from Celsius to Fahrenheit x=input("Input temperature in Celsius:") t=(x*1.8)+32 print "The temperature in Fahrenheit is:" elif z=='3': # The user wishes to quit the application t="Thank you for using the NetBeans Temperature Converter." else: e=raw_input("Invalid input, try again?(y or n)")
f==input_false if e=='y' or e=='Y': t='' temp() elif e=='n' or e=='N': t="Thank you for using the NetBeans Temperature Converter."
# Type validation code 1 here print t if z=='1' or z=='2': a=raw_input("Do you want to perform another conversion?(y or n)") if a=='n' or a=='N': t="Thank you for using the NetBeans Temperature Converter." elif a=='y' or a=='Y': t= ''
temp() # Type validation code 2 here temp()
|
-
In the Source Editor, type or paste the following code right before the second print statement, directly below the first # Type validation code 1 here comment. Then delete the comment.
else: t="You have entered an invalid option twice. The program will now close." |
-
Type or paste the following source code right above the closing method statement, directly below the # Type validation code 2 here comment. Then delete the comment.
else: t="You have entered an invalid option. The program will now close." print t
|
-
When finished, your code should resemble the following code sample. Make all necessary corrections before running the application.
def temp(): print "Welcome to the NetBeans Temperature Converter." z=raw_input("Press 1 to convert Fahrenheit to Celsius, 2 to convert Celsius to Fahrenheit, or 3 to quit:") a==no_input
if z=='1': # The user wishes to convert a temperature from Fahrenheit to Celsius x=input("Input temperature in Fahrenheit:") t=(x-32)/1.8 print "The temperature in Celsius is:" elif z=='2': # The user wishes to convert a temperature from Celsius to Fahrenheit x=input("Input temperature in Celsius:") t=(x*1.8)+32 print "The temperature in Fahrenheit is:" elif z=='3': # The user wishes to quit the application t="Thank you for using the NetBeans Temperature Converter." else: e=raw_input("Invalid input, try again?(y or n)")
f==input_false if e=='y' or e=='Y': t='' temp() elif e=='n' or e=='N': t="Thank you for using the NetBeans Temperature Converter." else: t="You have entered an invalid option twice. The program will now close." print t if z=='1' or z=='2': a=raw_input("Do you want to perform another conversion?(y or n)") if a=='n' or a=='N': t="Thank you for using the NetBeans Temperature Converter." elif a=='y' or a=='Y': t= '' temp() else: t="You have entered an invalid option. The program will now close." print t temp() |
- Right-click anywhere in the Source Editor and choose Format.
-
Now you determine if there are any unresolved names or unused variables in the application. In the main toolbar, choose Tools > Options.
The Options window opens.
-
Select the Python > Hints tab and make sure that Find Unresolved Names and Find Unused Variables are selected, as seen in the following image.

-
Click OK.
Errors are displayed in the Source Editor. To view the error message, place you cursor over the error or over the warning symbol
in the margin to the left of the error, as seen in the following images.
An unresolved name.

An unused variable.

- Delete the unresolved name and unused variable. They are not needed in this application.
Running the Project - 3
Now it is time to run your project. In NetBeans IDE, there are several ways to run your project. You can click the run icon in the main toolbar, or you can right-click the project node in the Projects window and choose Run.
-
In the Projects window, right-click the project node and choose Run.

The results are displayed in the Output window at the bottom of the IDE, as seen in the following image.
Debugging an Application
NetBeans IDE features a powerful Python debugger that helps you debug your application. This section of the tutorial describes how you can take advantage of this very useful feature. Depending on the size, complexity, and scope of the application, the Python Debugger offers various ways in which you can debug your project. For this simple application, however, many of these features are not necessary. This section of the tutorial covers only the basics of debugging in NetBeans IDE. A separate tutorial on debugging will be provided at a later date. For more detailed information about debugging an application, see JavaScript Debugger User's Guide
-
Before you start debugging the application, you can set the debugging options in the Options window. In the main toolbar, choose Tools > Options > Python and select the Debugger tab, as seen in the following image.
The default options are generally acceptable for a debugging session. However, you can choose to set several parameters.
- Debugger listening starting point. This field specifies the port number for the debugging process.
- Debugging shell colors and fonts. Use this group of buttons to specify the colors and fonts you want to use while debugging.
- Stop at first line. When selected, the debugger stops at the first line of the debugger's script. When deselected, the debugger stops at the first found breakpoint in the script.
-
Left-click in the left margin to set a breakpoint for debugging, as seen in the following image.

Start the Python debugger by right-clicking the project node in the Projects window and choosing Debug.
The Debugging window opens at the bottom of the IDE, as seen in the following image. Depending on the complexity of the application, other windows open as well.
-
By using the Debugging toolbar at the top of the IDE, you can step in, step out, and step over areas of code.
- After debugging your application, run the application and view the output in the Output window.
Summary
In this tutorial, you created a simple Python application using NetBeans IDE, added user input, and then added input validation. You experimented with code refactoring and code completion, and viewed the application's output in the Output window of the IDE. You also learned basic information on debugging an application in NetBeans IDE.
See Also
This page was last modified: December 10, 2009