Introduction to Python Application Development Using NetBeans IDE
Contributed by Josh Juneau. Edited by James Branam.
This tutorial is designed to show you the basics of using NetBeans IDE with the Python programming language. and so that they can begin to explore it's possibilities. With it's added support for various JVM and non-JVM languages, Netbeans opens new doors to programmers, giving them an easy-to-use environment for developing in many different languages including Python. Throughout this tutorial, you will be developing a small application (hockeyRoster.py) from the ground up. You will develop an application to contain and manage a sports team.
You need the Python EA download of the NetBeans IDE or the Python plugins installed to complete this tutorial.
Expected duration: 20 minutes
Contents
To follow this tutorial, you need the following software and resources.
Overview of the Application
In this introductory tutorial, you develop an application to manage a hockey team, a command-line application to add a list of players, their preferred position, and if their current status ("BENCHED" or "ACTIVE") to an external file. At that point, you will be able to list the contents of the file via Python as well as update records. This tutorial covers file I/O and does not delve into database specifics.
Setting Up
Before you create your application, make sure that you have Python installed on your machine. Then, download the Python Early Access (EA) version of the IDE, or install the NetBeans Python plugin in the Plugin Manager. The NetBeans Python plugin is easy to install and configure, and after installation, the IDE detects your various environments. You can configure the IDE for Python support in the Python Platform Manager. This window allows you to add or remove Python installations available to the IDE. Using the Python Platform Manager, you can also set up and configure your Python and Java paths. In doing so, you can place Java jar files or Python modules in your path so that they are available to use each time you start up the IDE. To take it one step further, you can even define different Java or Python paths for each installation you configure.
Adding a New Python Installation
-
In the main toolbar, choose Tools > Python Platforms Manager.
The Python Platform Manager opens, as seen in the following image.

-
If your Python installation does not appear by default, click New, browse to the installation you want to use and choose Open.
The IDE add the installation to the the list of Platforms. If you are unsure of the location of the installation, click Auto Detect.
Installing and Configuring your Python Installation
- Choose Tools > Python Installations to open the Python Platform Manager.
- Select the Python installation you want to use with the IDE and click Make Default.
- Enter any command arguments for the installation in the Command Arguments text field.
You can also rename the Platform name if necessary.
- Click Close to close the Python Platform Manager.
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 HockeyRoster as the Project Name, as seen in the following image. Select the version of Python you want to use with the project and rename the main file HockeyRoster.py.

- Leave the Set as Main Project checkbox and Create Main File checkbox selected. Click Finish
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. Note how your project is displayed in the Projects window.
Note that the HockeyRoster.py file is open in the Source Editor of the IDE, displaying basic information, as seen in the following image. Netbeans IDE automatically documents the author and date of the project along with providing a short sample print "Hello" program.

-
Now you create two source files for the application. In the Projects window, right-click the project's Sources node and choose New > Empty Module, as seen in the following image.
The New Empty Module wizard opens.
-
Type Player as the file name and click Finish.
The file opens in the Source Editor. The Projects window should now resemble the following image.
Adding Code
In this section, you add Python code to the files you created in the previous section of the tutorial.
-
If is not already apen, open the Player.py file in the Source Editor
-
Type the following code in the Player.py file.
# Player.py
#
# Container to hold our player objects
class Player:
# Player attributes
id = 0
first = None
last = None
position = None
# Function used to create a player object
def create(self, id, first, last, position):
self.id = id
self.first = first
self.last = last
self.position = position
|
The Source Editor should then resemble the following image. Note how the IDE assists you by entering parentheses and colons for you.
-
Open HockeyPlayer.py in the Source Editor.
-
Erase the existing code in the Hockeyplayer.py and type the following code. This first piece of code consists mainly of comments, but it also does two important things:
- It imports the Player class from the Player module we created previously.
- It defines a list to hold each of the Player objects.
# HockeyRoster.py # # Implementation logic for the HockeyRoster application
# Import Player class from the Player module from Player import Player
# Define a list to hold each of the Player objects playerList = []
|
-
Now you add more code to application. This piece of code creates a selector for the application by creating a function that prints output to the Output window, which is based on what the user enters.
# makeSelection() # # Creates a selector for our application. The function prints output to the # command line. It then takes a parameter as keyboard input at the command line # in order to choose our application option.
def makeSelection(): validOptions = ['1','2','3','4'] print "Please choose an option\n"
selection = raw_input("Press 1 to add a player, 2 to print the team roster, 3 to search for a player on the team, 4 to quit: ") if selection not in validOptions: print "Not a valid option, please try again\n" makeSelection() else: if selection == '1': addPlayer() elif selection == '2': printRoster() elif selection == '3': searchRoster() else: print "Thanks for using the HockeyRoster application."
|
-
Right-click anywhere in the Source Editor and choose Format, as ssen in the following figure.

-
Now you add code that accepts keyboard input from the Output window. The user enters
a player's first name, last name, and position.
# addPlayer() # # Accepts keyboard input to add a player object to the roster list. This function # creates a new player object each time it is invoked and appends it to the list.
def addPlayer(): addNew = 'Y' print "Add a player to the roster by providing the following information\n" while addNew.upper() == 'Y': first = raw_input("First Name: ") last = raw_input("Last Name: ") position = raw_input("Position: ") id = len(playerList) player = Player() player.create(id, first, last, position) playerList.append(player) print "Player successfully added to the team roster\n" addNew = raw_input("Add another? (Y or N)") makeSelection()
|
Note how the IDE's code completion makes suggestions for you as you type, as seen in the following image.
In this step you add code that prints the contents of the list as a report to the Output window.
# printRoster() # # Prints the contents of the list to the command line as a report
def printRoster(): print "====================\n" print "Complete Team Roster\n" print "======================\n\n" for player in playerList: print "%s %s - %s" % (player.first, player.last, player.position) print "\n" print "=== End of Roster ===\n" makeSelection()
|
Now you enter code that takes input for a player's name from the Output window and searches the roster list for a match.
# searchRoster() # # Takes input from the command line for a player's name to search within the # roster list. If the player is found in the list then an affirmative message # is printed. If not found, then a negative message is printed.
def searchRoster(): index = 0 found = False print "Enter a player name below to search the team\n" first = raw_input("First Name: ") last = raw_input("Last Name: ") position = None while index < len(playerList): player = playerList[index] if player.first.upper() == first.upper() or player.last.upper() == last.upper(): found = True position = player.position index = index + 1 if found: print '%s %s is in the roster as %s' % (first, last, position) else: print '%s %s is not in the roster.' % (first, last) makeSelection()
|
- At this point, it is time to add code for the application entry point. This code prints the application title to the Output window and then invokes the makeSelection() function.
# main # # This is the application entry point. It simply prints the applicaion title # to the command line and then invokes the makeSelection() function.
if __name__ == "__main__": print "Hockey Roster Application\n\n" makeSelection() |
Running the Application
Now it is time to test the application. In NetBeans IDE, resultsfrom Python applications are printed to the Output window.
-
In the Projects window, right-click the project node and choose Run.
The application appears in the Output window, as seen in the followiong image.

-
To test the appilcation, type 1 and press Enter.
You are prompted to enter a first name, last name, and position for the player you want to add, as seen in the following figure.

-
Try adding more players. Then, print the team roster by typing 2 and pressing Enter in the initial application prompt.
The roster is printed in the Output window, as seen in the following image.

-
Now search for a player by typing 3 and presing Enterin the initial application prompt.
The results are once again displayed in the utput window.

Now try searching for a player that you know is not in the roster.
The Output window informs you that the player is not in the roster.

Summary
In this tutorial, you created a simple Python application with user input using NetBeans IDE. You created a Python project, added an new empty module to the project, experimented with code completion, and ran your applcation, viewing the results in the Output window of the IDE.
See Also
This page was last modified: December 10, 2009