Referencing Python projects in Eclipse and PyDev

About this post

Suppose you are:

  • programming in Python
  • using the Eclipse IDE
  • with the PyDev plugin
  • and have many projects that are subsystems of a larger app or system.

This tells you how to join your subsystem projects using Eclipse ‘references’.

How to

I will call the two projects the ‘referring project’ (say an app) and the ‘referenced project’ (say a subsystem.)  By subsystem I mean a Python package that more or less stands alone and has a well defined API (that you or others might reuse in a larger project.)

To reference one project from another in Eclipse when using the PyDev perspective :

  • click RMB (right mouse button) on the referring project name in ‘PyDev Package Explorer’ panel.  A pop-up menu appears.
  • choose ‘Properties’ (near the bottom).  A ‘Properties’ dialog appears.
  • click LMB on ‘Project References’ in the list in the left panel of the dialog (the list is tree structured.)  A list of checkboxes appears in the right panel of the dialog.
  • Click on the project you want to reference (another PyDev project or maybe a C project if  it is a Python extension?)
  • Click on the OK button

Now when you run the referring project from within Eclipse, PyDev will set the PYTHONPATH system variable so that the referring project can import the referenced project.  (When you later install the referring project somewhere else, you will also probably include the referenced project in the ‘installation package’, and install both in the standard place.)

Caveat: subsystem projects that are not Python packages

The above works if the referenced project is a Python package (has an __init__.py file in the root directory.)  Suppose however, you have structured your referenced Python project tree like a C project (with a bin and source directory), with only the source directory being a Python package, e.g. with a path  like ‘myUsefulProject/myUsefulPackage.’  Then the above doesn’t work (PyDev only makes the PYTHONPATH include myUsefulProject but not myUsefulPackage.)  Instead of using references, you can set up the PyDev PYTHONPATH yourself:

  • click RMB (right mouse button) on the referring project name in ‘PyDev Package Explorer’ panel.  A pop-up menu appears.
  • choose ‘Properties’ (near the bottom).  A ‘Properties’ dialog appears.
  • click LMB on ‘PyDev PYTHONPATH’ in the list in the left panel of the dialog.  A set of tabs appears in the right panel.
  • choose the ‘External Libraries’ tab.  The right panel changes.
  • click the ‘Add source folder’ button
  • navigate the file browser to the subfolder of the referenced project that is a package (e.g. /home/<yourname>/git/myUsefulProject/myUsefulPackage)
  • click the OK button, and again

Note the warning “Changes in external libraries are not monitored, so, the ‘Force restore internal info’ should be used if an external library changes”.   This means, that as a syntax checker, PyDev does not monitor changes to external libraries.   So for example, if you delete a method from an external  library, PyDev will not show an error in a referencing project (at the source code line where it calls the deleted method.)   But when you run the referencing project, Python itself will find the error. (The difference is PyDev’s static checking versus Python’s dynamic checking.)

(As a piece of technical writing, that warning is poor.  It includes three passive verbs.  I would rewrite it “PyDev does not check for errors in referencing projects when you change referenced external libraries.  You should choose ‘Update PyDev’s syntax checking’ when you change the dictionary of external libraries.’)

Advertisements

One thought on “Referencing Python projects in Eclipse and PyDev

  1. Pingback: Linking Python projects in Eclipse with PyDev | plashless

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s