Eclipse PyDev IDE with OpenAccess

From oacwiki
(Redirected from PyDev)
Jump to: navigation, search

The PyDev Python IDE plugin for Eclipse makes developing OpenAccess applications with oasPython a snap.

Contents

Install Software

This turorial assumes the Linux platform. Start by installing the requisite software:

Verify that oasPython Runs Without PyDev

Before getting PyDev involved, verify oasPython in your shell environment by running one of the unit tests (bash shell syntax is used in this example):

 export PYTHONPATH=${OASCRIPT_ROOT}/python2.5:${PYTHONPATH}
 export LD_LIBRARY_PATH=${OA_ROOT}/linux_rhel40_64/opt
 python ${OASCRIPT_ROOT}/python2.5/test/util_test.py

where:

  • ${OASCRIPT_ROOT} is the location of oaScript (and ${OASCRIPT_ROOT}/python2.5 the location of oasPython)
  • ${OA_ROOT} is the location of OpenAccess

PyDev does not require that you set these environment variables globally (such as in ~/.bashrc). After it is set up, PyDev itself will contain the settings for PYTHONPATH and LD_LIBRARY_PATH.

If you get an error like,

 OSError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by ${OA_ROOT}/lib/linux_rhel40_gcc423_64/opt/liboaCommon.so)

your system does not have the version of libstdc++ required by OpenAccess. The same thing can happen with oasPython. See the instructions for installing oasPython for the solution to this problem.

Start Eclipse

With PYTHONPATH set as shown above, launch Eclipse:

 eclipse &

The Workspace Launcher dialog will ask you to specify a path to use as a workspace (default ~/workspace/). If you already have a directory like ~/src/ or ~/projects/ containing the source code for your various projects, specify that instead. Eclipse is a multilingual IDE—the projects need not all be Python projects.

Next, the Welcome to Eclipse screen is displayed. After you finish amusing yourself with the other cute options, select Workbench, in the upper right corner.

This is a good time to introduce the concept of Eclipse perspectives. A perspective is an arrangement of views and editors on the screen that are oriented toward some task, like editing Python source code or debugging. You can change the perspective using the widgets in the upper right:

Perspective chooser, with Open Perspectives icon on the left

Click the Open Perspectives icon (shown on the left in the above figure), choose Other > PyDev, and click OK. This switches the perspective to PyDev.

Unless you will be needing it soon, right click on the Java perspective and Close it. You can always open it later using the same procedure you just used to open the PyDev perspective.

Set Up a Python Interpreter

In the Eclipse menu, choose Window > Preferences. The Preferences dialog will appear.

Navigate to PyDev > Interpreter - Python. Click New:

  • Specify the Interpreter Name that you want to use for the interpreter within Eclipse, like Python2.7.1. No spaces are allowed.
  • Specify the full path to your Python Executable, like /usr/local/bin/python.
  • Click OK

Eclipse will search your Python installation and your PYTHONPATH environment variable for directories containing Python modules. Note that this is the only time that PYTHONPATH from your shell environment is referenced. Later, when you run or debug your programs, only PYTHONPATH as defined within PyDev is used. Of course you can later edit the PYTHONPATH in your Python Interpreter.

PYTHONPATH

In the Libraries panel Eclipse displays a list of the System PYTHONPATH that it found.

Before you started Eclipse, you set PYTHONPATH to include oasPython, so make sure it appears in the list. If for some reason this did not work, you can also add it manually.

LD_LIBRARY_PATH

Click on the Environment panel. Click New:

  • Enter Name LD_LIBRARY_PATH
  • For Value, enter the value of LD_LIBRARY_PATH that worked when you verified that oasPython runs
  • Click OK

Click OK to dismiss the Preferences dialog. PyDev will take some time to index the contents of the files in System PYTHONPATH.

The Purpose of the PyDev Interpreter

The Python Interpreter captures a set of global settings:

  • The full path of the Python interpreter
  • PYTHONPATH
  • LD_LIBRARY_PATH

These settings will be used when Python programs are run inside PyDev, regardless of how the environment variables are set at run time. You will specify a Python Interpreter for each PyDev Project.

Add New Projects to the Workspace

The next step is to add PyDev projects. This section describes how to create new projects; the next section describes how to create projects for your existing source code.

Each project occupies a directory within the workspace directory.

In the Eclipse menu, choose File > New > PyDev Project (or alternatively, File > New > Project, and choose PyDev > PyDev Project).

The PyDev Project dialog appears:

  • Type a Project Name
  • Unless you have a good reason not to, Use default for the Project contents so that the directory containing the project will be the same as the project name
  • OpenAccess oasPython is based on native Python, so choose Python as the Project Type
  • Choose the Grammar version you want to use in the Python code you write
  • Choose the Interpreter that you previously set up. Avoid using the Default interpreter.
  • If you want to put your source files into a src/ directory, select Create 'src' folder and add it to the PYTHONPATH?
  • If you want to put your source files directly into the project folder, select Add project to the PYTHONPATH?
  • Click Finish

An empty project will appear in the PyDev Package Explorer on the left.

You can add new files and folders by selecting on a folder, right clicking, and selecting from the context menu New > File or New > Folder.

Add Existing Projects to the Workspace

You can also create PyDev projects for existing directories containing Python source code:

  • Each project directory workspaceName/projectName/ must contain one and only one project
  • Although not required, it is more convenient if the source code is either in the project directory workspaceName/projectName/ or in workspaceName/projectName/src/

To add an existing project, follow the procedure for adding a new project as if you were adding a new project with the same file structure. In the PyDev Project dialog just make sure to:

  • Use a Project Name identical to the name of the directory containing the existing project
  • If your existing project's Python source files are in workspaceName/projectName/, select Add project to the PYTHONPATH?
  • If your existing project's Python source files are in workspaceName/projectName/src/, select Create 'src' folder and add it to the PYTHONPATH?.

The new project will appear in the PyDev Package Explorer on the left, populated with your existing sourde code files.

  • Expand the project and folders to see the files within
  • Python source code files containing errors will be marked with a red "X"
  • Open a file by double clicking on it
  • Errors within the source files are marked with a red "X". Hover over the "X" to see an explanation of the error.

The figure below shows the file MargaritaMixer.py open. PyDev has detected an error in the file. The error is explained in the yellow box.

Hovering over an error marker causes an explanation to appear (in the yellow box)

Add Files to Your Version Control System

There is an Eclipse plugin for virtually every version control system. To set up, use the Window > Preferences menu command. The version control preferences are under Team.

To execute version control commands on individual files or folders, select them in the PyDev Package Explorer, right click, and in the context menu, choose from the version control commands in Team.

You may also use your version control system's command line interface whenever you like. Eclipse won't mind.

In addition to your Python source files, check in the .project and .pydevproject files that Eclipse creates in the directories within your project. Do not check in the .settings/ directory.

Edit PYTHONPATH

The error in the above figure indicates that PyQt4 is missing from PYTHONPATH. You can make it available to all projects that use a given Python interpreter or just an individual project.

System PYTHONPATH

If you want to make a path available to all projects that use a given Python interpreter, choose Window > Preferences in the Eclipse menu. The Preferences dialog will appear.

Navigate to PyDev > Interpreter - Python.

  • Highlight the Python interpreter you want to change
  • Select the Libraries panel
  • Click New Folder
  • A browser will appear:
    • Specify the path you want to add
    • Click OK
  • Click OK

Project PYTHONPATH

If you want to make a path available to a single project, In the PyDev Package Explorer (on the left), select the project. Right click and choose Properties from the context menu. In the Properties dialog, navigate to PyDev > PYTHONPATH:

  • If you are adding an external library like PyQt4, add it in the External Libraries panel
  • If you are adding another source directory within your project (like projectName/src), add it in the Source Folders panel
  • Click OK

Edit Source Files

In the PyDev Package Explorer, double click on a file to open it in an editor. The editor is context sensitive and you can change its behavior.

Navigate to the definition of a function by pressing F3, and display the hierarchy view using F4.

Debug a Program in PyDev

The most exciting part of using an IDE is running programs in the debugger. The PyDev Manual contains a fine description of how to use the PyDev debugger.

Start the Debugger

Open a source code file in an editor and select it by clicking on it. Select Run > Debug from the menu. The first time you do this, the Debug As dialog appears, asking whether you want to do Python Run or Python Unit Test. If the file contains a unit test, select Python Unit Test, otherwise Python Run.

PyDev sets up a Debug Configuration and executes the file. The next time you run this file, PyDev uses the existing Debug Configuration.

Edit the Command Line Options and Working Directory

The initial Debug Configuration was created from the Python Interpreter and project Preferences, but it can be edited.

The most common reasons to edit a Debug Configuration are to set a working directory and add command line arguments to be applied when the program runs. To do this select Run > Debug Configurations from the menu:

  • Choose the run configuration to edit using the list on the left. Ordinarily it will be named projectName sourceFileName.py.
  • Select the Arguments pane
  • In Program arguments, add the command line arguments for your program
  • In Working directory, select Other and enter the working directory you want. For installation independence, these are ordinarily specified using Eclipse variables.
  • Click Apply
  • Click Run to run the debugger, or Close if you do not care to debug now

Run a Program in PyDev

You can also run your programs within PyDev without the debugger. The procedures are virtually the same as described above. Just replace "Debug" with "Run".

Optional Settings

Searching Within Dialogs

The preference dialogs contain a bewildering array of options, so it can be hard to find the one you want. In the upper left corner of each preference dialog, there is a search box. Type a few characters of the attribute you want to set, ant the choices will melt away.

Try it. Go to Windows > Preferences. In the upper left corner, type number. The pane on the left is reduced to the Text Editors options, which contains Show line numbers.

Line Numbers

Display source code file line numbers using Windows > Preferences > General > Editors > Text Editors. Check Show line numbers.

Replace Tabs with Spaces

The Style Guide for Python Code recommends using four spaces per indentation level. If you would prefer to type these spaces using the Tab key, go to Windows > Preferences > PyDev > Editor and check Replace tabs with spaces when typing.

Dealing with Problems

The error,

 RuntimeError: OpenAccessException (3006) : Unable to create library testLib at path ./testLib: Non-empty Directory

probably means that somewhere you have a design in this library open, like in Virtuoso. Close any such designs and try again. (repeat this in the unit testing doc)

The error,

 RuntimeError: OpenAccessException (2003) : Attempt to save a read-only technology database

probably means that you are trying to run your program while you still have another process running yet paused in the PyDev debugger. Terminate the debugger process using the Run > Terminate command and try again.

If you get an error like,

 OSError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by ${OA_ROOT}/lib/linux_rhel40_gcc423_64/opt/liboaCommon.so)

your system does not have the version of libstdc++ required by OpenAccess. The same thing can happen with oasPython. See the instructions for installing oasPython for the solution to this problem.

-- JohnMcgehee on site at Altera

Personal tools