Getting Started With Python in the NetBeans IDE 6.5
This tutorial shows Python developers how to work with Python in the NetBeans IDE 6.5. It covers how to set up which Python runtime you want to use, how to create a new project, how to edit files, and how to run your programs. Use the NetBeans Python Editor for code completion, instant rename, quick fixes, catching unresolved or unused names, and import management. NetBeans also supports an interactive console and a debugger for Python.
When you are working with a Python project in NetBeans, the first thing to do is to create a project. The project keeps things organized and also keeps track of command line arguments, which Python runtime you are using, and other important data.
Setting Your Platform Runtime
Python support in NetBeans allows you to choose the version of Python used to run the Project. Currently, NetBeans comes with Jython 2.5 (alpha 3) pre-installed, but you can use any version of Python that is installed on your computer. Let's assume that you already have Python installed somewhere on your machine, and that you know in which directory Python is installed.
In this dialog you create an entry for a version of Python (you can see that there is already an entry for Jython), or you pick one of the existing version entries for your project. You can also set a particular version as the default version for new Python projects.
The IDE displays your selection in the Command and Console Command fields of the dialog box.
You can edit these fields, and you can also supply any command line arguments to be passed to the interpreter. The Python Path tab allows you to add additional Python libraries to your Python path. The Java Path tab allows you to add new Java libraries to the Java classpath used by Jython. When you are finished, click Close.
Starting a new project
NetBeans uses projects as a way to keep all the files for a project organized, and the first thing that you need to do is create a project for your work. To create a new project:
1. Choose File > New Project from the main menu.
The New Project wizard appears.
2. If you want to start from scratch, select New Project. Enter a project name and destination, and select the Python runtime you want to use to execute the project files. Click Next
If you have existing Python files that you want to import into a project, select the Python Project with Existing Files option in the New Project wizard. Click Next
3. If you selected Python Project with Existing Files in the previous panel, specify the directory or directories of your existing Python files. Click Finish.
Adding a new file is simple.
1. Make sure that your project is selected, and then choose File > New File from the main menu.
2. Select either Empty Module or Executable Module.
NetBeans prepopulates the new file with common information.
Here are some of the major editing features of NetBeans IDE Python Early Access.
At any time when typing in the editor you can press Ctrl-Space, and the NetBeans IDE displays a list of possible completions. You can do this in the middle of typing a word, and completions will be reduced to actual completions of what you have typed. Use the arrow keys to scroll through the list of completions, and if you select a function, notice that the documentation of the function is also displayed.
If you place your cursor before an identifier, NetBeans Python highlights all the related usages (and the definition) for that identifier. You can rename all those usages instantly by typing Ctrl-R. When you do this, a red box appears around the identifier where ever it is used and where it was defined. As you type over the contents of the box, all the highlighted occurrences are updated as well.
NetBeans Python can point out areas where your code could be improved. If it has such a suggestion, a yellow light bulb appears in the column just to the left of the code entry area.
Click the light bulb to get more details on the suggested improvement, and press Enter to execute the suggested fix.
Catching unresolved or unused names
The NetBeans IDE for Python makes it easy to see if you have a variable that isn't being used (likely the result of a typo) or have used a name that doesn't exist (can't be resolved - and also likely the result of a typo). Both of these features need to be turned on, and you'll find the settings in the Hints section of the Editor pane in the Options window. To access the Options window, choose Tools > Options in the main menu. For unused variables, the preference is here:
The indicator resembles this.
The preference for unresolved names resembles this.
The indicator looks like this.
Another really handy feature of the NetBeans IDE for Python is Import Management. Place the cursor over a name, right-click, and choose Fast Import.
The IDE displays a list of all the modules that define that name.
Another useful feature is Fix Imports, which takes import statements
and cleans them up.
There are several ways to run your Python program. One of these is to set one file as the main file for the project. To do this:
1. Right-click the project node in the Projects window, and select Run.
2. Specify the name of the main file, along with any command line arguments that are to be passed to that file.
If you set your project to be the Main Project (Run > Set Main Project > <your project name>), then you can run the main file by choosing Run > Main Project in the main menu, or by pressing F6. You can also right-click any file in the Projects window and choose Run File (the shift-F6 key). The output of your program appears in the Output window at the bottom of the IDE.
NetBeans Python also provides you with access to the Python Interactive Console. You can open the Interactive Console by choosing Window > Python Console from the main menu.
The Console window appears in the Output window at the bottom of the IDE. You can then type Python code into the console to execute it. You can also interrogate the state of the Python interpreter through this console.
The NetBeans IDE for Python includes a Python source code debugger. Jean-Yves Mengant, the author of the debugger has written an excellent tutorial.