"Go" PyDev for Eclipse Introduction

Source: Internet
Author: User
Tags class definition ibm developerworks

Transferred from: http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-pydev/index.html

PyDev for Eclipse is a powerful and easy-to-use eclipse Python IDE plugin. This article will introduce the reader to the PyDev Open source project and its installation configuration method, and on this basis detailed how to use the PyDev plug-in to turn Eclipse into a powerful and easy-to-use Python IDE, how to use it for the development and debugging of Python programs. Through this article, the reader can not only understand PyDev this open source project, but also have a better understanding of how to apply the PyDev plugin to use Eclipse as a Python IDE for the development and debugging of Python applications.

————————————————————————————————————————————————————————————————————————————

PyDev Introduction

On July 16, 2003, the three-person development team, led by Fabio Zadrozny, registered a new project on the world's largest open source software development platform and warehouse SourceForge, which implemented a powerful eclipse plugin that allows users to take full advantage Eclipse for the development and debugging of Python applications. The project that can use eclipse as a Python IDE is PyDev.

The advent of the PyDev plugin makes it easy for many Python developers to provide some good features such as syntax error hints, source code editing assistants, Quick Outline, Globals Browser, Hierarchy View, run and debug, and more. Based on the Eclipse platform, which has many powerful features and is also very easy to use, PyDev's features make it more and more popular.

Today, the project is continuing to promote the new release version, the latest version is released on October 3, 2008, 1.3.22. This article will introduce the installation configuration method for PyDev, and on this basis, it details how to use PyDev to develop and debug python as a Python IDE.

PyDev Installing and configuring the installation PyDev

Before installing PyDev, ensure that you have installed Java 1.4 or later, Eclipse, and Python. Next, start installing the PyDev plugin.

    1. Start Eclipse and install PyDev with Eclipse Update Manager. Find the Help bar in the Eclipse menu bar and select Help > Software Updates > Find and Install.
    1. Select Search for new features for install, and then click Next. In the window that appears, select New remote site. A dialog box pops up asking you to enter the name and link of the new update site. Here, the name entry PyDev, of course, you can also enter a different name; link input http://pydev.org/updates. Then, click Ok.
Figure 1. New Update site

    1. In this way, a new PyDev site is built, select the site, and then Finish. Next, Eclipse's update Manager will search for the installation package in the site you just entered, check the results of the search PyDev, and click Next.
Figure 2. Installing Pydev

    1. Read the license Terms and, if accepted, click Next. Go to the Installation Path selection screen, use the default settings, and then Finish.
    1. Eclipse Update Manager Downloads PyDev and you can see the progress of the download from the Eclipse taskbar. After downloading, display a screen that requires you to confirm the installation, click Install all to start the installation.

After installation, you need to restart Eclipse for the installation to take effect.

Verify that PyDev is installed successfully

How can I verify that Eclipse Update Manager has successfully installed the required PyDev plugin?

Selecting Help->about Eclipse sdk->plug-in Details will appear in the About Eclipse SDK plug-ins window, which lists all installed Eclipse plugins. Check if there are at least five plugins in the plug-in Id column that start with Com.python.pydev and Org.python.pydev respectively. If so, then the Pydev has been successfully installed, otherwise, the installation of some problems, need to be based on specific problems to do specific analysis.

Figure 3. Verifying the PyDev Plugin

Configure PyDev

After installing the PyDev, the Python/jython interpreter needs to be configured and the configuration process is simple.

In the Eclipse menu bar, choose Window > Preferences > Pydev > Interpreter-(Python/jython), where the Python/jython interpreter is configured, below with Python For example, describe how to configure.

You first need to add an already installed interpreter. Here, Python is installed under the C:\Python25 path. Click New, select the Python interpreter python.exe, open a window with many checkboxes, select the path you want to add to the system Pythonpath, and click Ok.

Figure 4. Configure PyDev

Next, check that the configuration results are correct.

In System Pythonpath, check to see if the paths that were added to the configuration process are included. Here is a list of all the required library folders for the system.

In addition, in the forced Builtin Libs, a Python built-in library is listed. For Python, there are about 50 built-in libraries, and for Jython, there are about 30.

In this way, the Python interpreter is configured.

Back to top of page

PyDev Package Explorer to create a project

Before you start, you need to create a new project. In the Eclipse menu bar, choose File > New > Project > Pydev > Pydev Project, click Next.

Figure 5. Create a Pydev Project

At this point, display the Pydev Project window, enter the project name, select the work path, select the version type of the Python interpreter and select the check box, then click Next to enter the associated project window, and if you do not need to associate other projects, you can click Finish to complete the project creation.

Creating Python Packages and modules

Next, start creating Python packages and modules in the project you just created.

    1. To enter the Pydev perspective, in the Python package Explorer, right-click SRC, select New->pydev packages, enter the bundle name and click Finish,python Pack to create it, and automatically generate __ init__.py file, which contains no content.

Note: If the CREATE default SRC folder and add it to the Pythonpath check box is not selected when creating the project, you need to pass File > New > Other > Source Fo Lder manually create a source code folder.

    1. After you have created the Pydev package, right-click the packages you created, select New->pydev module, enter a module name, and click Finish. In this way, the Python module is built.
Edit source Program

For some basic editing methods of the source program, do not introduce. Here are some useful editing features provided by Pydev.

    1. Syntax error hints

Python developers in the process of creating a modified program, if it can be found in time during the editing process of grammatical errors, no doubt the quality and progress of the overall project development is very important. In the Python perspective, the source code for the project is listed in the Pydev package Explorer, double-clicking one of the Python files, and if the file contains syntax errors, the error is displayed in a very visible way.

Figure 6. Pydev File Syntax error hints

If you want to display all the files in the entire project that contain syntax errors, you can switch freely from the Python perspective to the Java Perspective view. In the Java package, a striking little red fork marks all python files that contain syntax errors.

Figure 7. Pydev Project Syntax error hints

    1. Source code Editing Assistant (Content assistents)

The source editing assistant (Content assistents, hereinafter referred to as CA), as the name implies, is used to help developers to edit the source program, which provides many convenient and practical functions to guide the developers to develop the project efficiently and quickly.

The CA can be activated via the shortcut key ctrl+1, which supports the following functions:

PyDev

    1. Move Import to global scope
    2. Create docstring
    3. Assign result to new local variable (or field)
    4. Assign parameters to attributes
    5. Surround code with try: Except or try: Finally

PyDev Extensions

    1. Make Import for undefined token
    2. Ignore Error
    3. Don ' t analyze module

The PyDev and PyDev Extensions packages are installed when PyDev is installed, so the major features of the CA are now supported. First, we will first describe how to use the CA features included with PyDev.

    • PyDev CA Functionality
    1. Move Import to global scope

Take the following code example, after moving the cursor to import sys, start the shortcut key ctrl+1 activate the CA, "Move Import to Global scope" appears in the label, press Enter to apply this feature. If you do not want to apply this feature, you can press the ESC key to cancel.

#!/usr/bin/python–usys.path.append ("./virtualfs") Import sys

When this feature is applied, the import SYS is successfully moved to the global scope, eliminating the previous error. The code after the change is as follows.

#!/usr/bin/python–uimport syssys.path.append ("./virtualfs")
    1. Create docstring

The Create docstring feature can automatically add parameter annotations to the function. Move the cursor to the following line of code, launch the shortcut key ctrl+1 activate the CA, and "Make DocString" appears in the tab bar.

def __init__ (self, dbUser, dbpswd, Dbhost):

When you press Enter to apply the feature, parameter annotations are automatically added to the function.

def __init__ (self, dbUser, dbpswd, dbhost): "@param virtualoperator: @param database: @param hostname: @param workdir:"
    1. Assign result to new local variable (or field)

The CA also provides a function to assign a function return result to a new internal variable. Take the function Callmethod as an example and move the cursor to the A.callmethod () line to activate the CA.

     Def method (self, a): A.callmethod ()

Select "Assign to Field (self, callmethod)" or "Assign to local (Callmethod)", you can assign the A.callmethod () result to the new internal variable Self.callmethod, the changed code is as follows.

     Def method (self, a): Self.callmethod = A.callmethod ()
    1. Assign parameters to attributes

In the process of program editing, if you need to assign function parameters to variables, you can use the CA's Assign parameters to attributes function to automate such requirements. Move the cursor to the line where the function M1, activate the CA.

    Class Foo (object):D EF M1 (self, A, b):

Select "Assign parameters to Attributes" in the tab bar to automatically generate two lines of code to assign the parameter A, a, to the variable with the same name.

Class Foo (object):D EF M1 (self, A, b): SELF.A = aself.b = b
    1. Surround code with try: Except or try: Finally

For code that might produce an exception, the exception is caught, usually using try: Except or try. Finally statement to catch the exception. Select a code print usage to activate the CA's "Surround code with Try". Except or try: Finally feature, you can automatically make exception captures of print usage.

Import Sysdef method (self, usage):  try:      print usage  except:      raise

The following is a description of how the CA functionality that PyDev Extensions contains is used.

    • PyDev Extension CA functionality
    1. Make Import for undefined token

Take the following code example, XmlReader undefined, parsing error.

     Class Test:def method (self):     XmlReader

Move the mouse to the error line, launch the shortcut key ctrl+1 activate the CA, select "Import XmlReader (Xml.sax)" in the tab bar, automatically generate a line of code from Xml.sax Import XmlReader, syntax error elimination.

From Xml.sax import XmlReader  class Test:def method (self):     XmlReader
    1. Ignore Error

Still take the above code as an example, because XmlReader is not defined, contains syntax errors, in this line to activate the CA, select "Undefinedvariable", syntax errors are ignored, XmlReader automatically generate a line of comments marked "#@ Undefinedvariable ".

  Class Test:def method (self):     XmlReader # @UndefinedVariable
    1. Don ' t analyze module

The parser can help display code that contains syntax errors, but in the process of program editing, it is sometimes necessary to deliberately de-parse the program, and the CA's Don ' t analyze module provides such functionality.

Move the cursor to the first line of the program, activate the CA, select "@PydevCodeAnalysisIgnore", automatically generate a line of code "#@ Pydevcodeanalysisignore", ignoring the parsing of the program body.

# @PydevCodeAnalysisIgnore   class Test:def method (self):          XmlReader
    1. Quick Outline

For a specific Python file, the quick Outline provided by Pydev Extensions is the simplest and quickest way to get the organization of the file, and to easily query the information needed for locating it in the file.

In the Pydev perspective, select the Show quick Outline, Source, or use the shortcut key Ctrl+o to start the feature.

The structure of the Python file's classes, functions, and so on is graphically presented in a tree-like form. At the same time, Filter provides the function of query positioning, which makes it easy to query the required information and locate the appropriate code snippet.

Figure 8. Quick Outline

    1. Globals Browser

Globals Browser is another powerful query location feature provided by Pydev Extensions. It can query and locate some definitions and attributes within the entire project, including:

    • Class definition
    • Method definition
    • Global variables
    • Class and Instance Properties

This feature can be started in three ways.

    • In the Pydev perspective, select Pydev, Globals Browser from the menu bar.
Figure 9. Menu bar Start Globals Browser

    • In the Pydev perspective, the toolbar is like a small icon below which the mouse moves over the icon to display the Pydev:globals Browser callout. Click the icon button to start the Globals Browser function.
Figure 10. Toolbar start Globals Browser

    • With the shortcut key CTRL + Shift + T, you can quickly start the Globals Browser feature.

Enter the definition, variable, or property you want to query in the Filter, and Globals Browser can quickly navigate to the appropriate code snippet.

Figure 11. Globals Browser

    1. Hierarchy View

When a Python file contains multiple classes, how can you easily and intuitively understand the dependencies between classes? Hierarchy View provides the ability to visually visualize hierarchical relationships between multiple classes in a tree-like structure.

As an example of a Python code, the Super1, Super2, Toanalyze, and Sub1 four classes are defined. In the Pydev perspective, select Windows, Show view, and other, and in the pop-up Show view window, select Pydev-Hierarchy view. By pressing the shortcut key F4 to activate Hierarchy View, you can see that the hierarchical relationship between classes is shown in the tree view.

Figure 12. Displaying hierarchical relationships of classes in Hierarchy View

Hierarchy View also supports the following four features:

    • In the hierarchy chart, click a class with your mouse, and the method for that class is displayed below the diagram.
    • If you double-click a class, method, or property, you will bring up the source program and enter the editing state for that class, method, or property.
    • In Hierarchy View, holding down the right mouse button and moving the mouse against or to the right, the hierarchy is scaled down or enlarged accordingly.
    • In Hierarchy View, press and hold the left mouse button to move the mouse, the hierarchy will be arbitrarily dragged to the appropriate location.

Back to top of page

Running and debugging a running program

To run the Python source program, there are two methods to choose from. The following is an example of a code example.py that describes both modes of operation.

    • Double-click example.py in the Pydev package Explorer and select Run as---Python run. The program example.py is immediately run and shows the execution results of the program in console consoles.
Figure 13. Python Program and running results

    • In the Pydev package Explorer, right-click example.py, and in the menu bar that pops up, select Run as, Python run. Similarly, the example.py is executed, and the Console displays the execution results of the program.

Both of these methods are the basic methods of running the source program. Pydev also provides a unique run as Python coverage of the source program, which not only shows the results of the program, but also shows the code coverage during the program's run.

To see the coverage of your code, you first need to open a code coverage Results view. In the Pydev perspective, select the Windows, Show View, Code Coverage Results View. In the left column of the pop-up view, you can see three buttons, "Choose dir!", "Clear coverage information!" and "Refresh coverage infomation".

Figure 14. Code Coverage Results View

With the left mouse button click "Choose dir!", in the pop-up Folder Selection window, select the package that contains the program you want to run, click OK. This way, all the source programs in this package are displayed in the left column.

Next, take example.py as an example to see the results of the Run as Python Coverage feature. Select Run as, Python Coverage, which shows the results of the program running in console console. Switch to the Code Coverage Results view that you just opened, and click Example.py in the left column.

Figure 15. To show code coverage in the Results View of a code coverage

The coverage in the process of running the code is clearly displayed in the right column.

Double-click the example.py in the left column, and the code that is not covered is marked with an eye-catching error mark in the editor.

Figure 16. Show code that is not overwritten with an error flag

If you turn off the code coverage Results view, the overwrite information is not lost, and reopening the view can also show this information. You can clear these overwrite information after the program runs only by clicking the Clear Coverage information! button in the left column.

Debug program

Debugging is essential in the process of program development, and mastering the debugging skills is the prerequisite and foundation for developers to develop efficiently. The following still takes example.py as an example of how to use the debugging features of Pydev.

Debugging starts with adding breakpoints and there are three ways to set breakpoints.

    • Double-click the left gray blank bar on the ruler bar in the editor to add a breakpoint to a line.
Figure 17. Double-click on the left gray blank Bar of the ruler bar to add breakpoints

    • Right-click the ruler bar and select "Add Breakpoint" in the popup menu bar to add a breakpoint.
Figure 18. Right-click the ruler bar to add a breakpoint

    • Move the mouse to the line of code where you want to add a breakpoint, use the shortcut key CTRL+F10, and select "Add Breakpoint" in the popup menu bar to add a breakpoint.

After adding a breakpoint, select Debug As, Python Run to start the debugger, pop up a dialog box asking whether to switch to the debugger perspective, click Yes to display debug mode.

Figure 19. Debugger Perspective

In the process of debugging the program, a few common shortcuts are as follows:

    • Stepping into step into:f5
    • Single Step Skip step Over:f6
    • Single step back to step RETURN:F7
    • Start Resume:f8 again.

In console console, displays the execution result of the code before the breakpoint. If you want to see the value of a variable, take variable A as an example, you can manually type a line of code "print ' A is: ', a" in the console, and then press the Enter key two times to display the value of the variable.

Figure 20. Console display variable values

In debug mode, to view the value of an expression, select the right mouse button and select Watch. The expression panel pops up, showing the value of the corresponding variable or expression.

Figure 21. Display expression values in the expression panel

You can set the properties of a breakpoint if you want to have a breakpoint that has already been added under certain conditions to be valid. Right-click in the ruler bar of the editor and select Breakpoint Properties in the menu bar that pops up. In the window that appears, select the check box "Enable Condition", enter the criteria you want to meet, and click Ok.

Figure 22. Set breakpoint Properties

This allows the breakpoint to be valid only if the condition is met when the program is re-debugged.

Back to top of page

Summarize

Pydev combined with ecplise to achieve such a powerful and easy-to-use Python IDE, this article can not be readily introduced, for some basic functions have not done too detailed introduction, the main highlight Pydev unique features. The advent of Pydev for Eclipse provides a good condition for Python developers to achieve efficient project development, and the project is constantly evolving, and its capabilities will become more and more powerful.

Resources
    • Python topics include the cute Python series, the Python Discovery series, Python extensions, the use of Python for various application development, and more.
    • View the Eclipse recommended reading list.
    • Browse all Eclipse content on the DeveloperWorks.
    • Are you a novice to Eclipse? Read the DeveloperWorks article "Getting Started with Eclipse platform" to learn about its origins and architecture, and how to extend eclipse with plug-ins.
    • View IBM DeveloperWorks's Eclipse project resources to improve your eclipse skills.
    • Visit the DeveloperWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technology and use it with IBM products.

"Go" PyDev for Eclipse Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.