NetBeansPythonTutorial

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.

Contents


Projects

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.

1. Choose Tools > Python Platforms from the main menu.
The Python Platform Manager dialog appears.
Image:default-python-platform-manager_NetBeansPythonTutorial.png

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.

2. To create a new Python version entry, click New. Type the path or browse to the location of the Python interpreter on your computer, and click Open.
Image:select-new-python-platform_NetBeansPythonTutorial.png

The IDE displays your selection in the Command and Console Command fields of the dialog box.

Image:newly-added-python-platform_NetBeansPythonTutorial.png
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.


Image:new-project-dialog_NetBeansPythonTutorial.png
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
Image:new-python-project_NetBeansPythonTutorial.png
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
Image:new-python-project-with-existing-sources_NetBeansPythonTutorial.png
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.
Image:select-sources-for-new-project_NetBeansPythonTutorial.png


Creating/adding files

Adding a new file is simple.

1. Make sure that your project is selected, and then choose File > New File from the main menu.
Image:new-file-dialog_NetBeansPythonTutorial.png
2. Select either Empty Module or Executable Module.
Image:new-empty-module_NetBeansPythonTutorial.png
NetBeans prepopulates the new file with common information.
Image:default-file-contents_NetBeansPythonTutorial.png

Editing

Here are some of the major editing features of NetBeans IDE Python Early Access.

Code Completion

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.
Image:code-completion_NetBeansPythonTutorial.png


Instant Rename

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.
Image:instant-rename_NetBeansPythonTutorial.png

QuickFix

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.
Image:quick-fix-bubble_NetBeansPythonTutorial.png
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:

Image:find-unused-variables-pref_NetBeansPythonTutorial.png

The indicator resembles this.

Image:unused-variable-indicator_NetBeansPythonTutorial.png

The preference for unresolved names resembles this.

Image:find-unresolved-names-pref_NetBeansPythonTutorial.png

The indicator looks like this.

Image:unresolved-name-indicator_NetBeansPythonTutorial.png

Import Management

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.

Image:fast-imports-menu_NetBeansPythonTutorial.png

The IDE displays a list of all the modules that define that name.

Image:fast-imports-dialog_NetBeansPythonTutorial.png

Another useful feature is Fix Imports, which takes import statements

Image:fix-imports-before_NetBeansPythonTutorial.png

and cleans them up.

Image:fix-imports-after_NetBeansPythonTutorial.png


Running Programs

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.
Image:run-properties-main-file_NetBeansPythonTutorial.png
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.
Image:run-output-area_NetBeansPythonTutorial.png

Interactive Console

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.
Image:console-menu_NetBeansPythonTutorial.png
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.
Image:console-output_NetBeansPythonTutorial.png


Debugger

The NetBeans IDE for Python includes a Python source code debugger. Jean-Yves Mengant, the author of the debugger has written an excellent tutorial.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo