Monday, 5 September 2011

How to share Python code with PyDev for Eclipse

Python allows sharing the code between projects. Shared code (classes, functions,...) is grouped by its purpose/nature into modules which are imported into projects. Modules are simple text files containing Python code and they have extension .py. Modules are organized into packages. Package is actually a directory which contains file, modules and/or other packages.

Let us create package named common which contains one module, crypto.

File->New->Python Project

Project Name: common
Directory: C:\Users\Bojan\workspace\Python\common
Project type: Python
Grammar version: 2.7
Interpeter: Default
Create 'src' folder and add it to the PYTHONPATH

This creates following file hierarchy on the disk:

   - src (directory)
   - .project
   - .pydevproject

Packages/modules are usually added as subdirectories of src directory. If any other Python project wants to use any package from this project, it needs to add absolute path to common\src to its PYTHONPATH.

File->New->PyDev Package

This wizard forces us to pick src folder in some project as Source Folder:
Source Folder: /common/src
Name: util

File hierarchy on the disk now looks like this:

   - src (directory)
      - util (directory)
   - .project
   - .pydevproject

A new directory - util - has been created and file has been automatically added to it. It is empty by default and we can leave it as such. This file marks its parent directory (util) as Python package directory

Let us add module crypto to util package:

File->New->PyDev Module:
Source Folder: /common/src (wizard allows only src directories form Python projects to be selected here)
Package: util (from /common/src)
Name: crypto
Template: empty

This adds file to util directory. Hierarchy on the disk is now:

   - src (directory)
      - util (directory)
   - .project
   - .pydevproject

If path to common\src is added to PYTHONPATH in some Python project, any Python file belonging to it is able to import crypto module from util package by including this line:

from util import crypto
from util.crypto import *

PYTHONPATH is project specific and PyDev uses it to locate source files for compiling, running autocompletion and debugging.

Let's add some function to

import hashlib

def get_md5(file_path):
    md5 = hashlib.md5()
    with open(file_path,"rb") as f: 
        for chunk in iter(lambda: * md5.block_size), ''): 
    return md5.hexdigest()

Let us create now a new Python project (named md5) which uses get_md5 function from util.crypto module: 
File->New->PyDev Project, Name: md5. We now need to add path to desired module to PYTHONPATH for this project: select project name in the Navigator pane; right-click, Properties; select PyDev - PYTHONPATH; select "External Libraries" tab (as we're adding source that does not belong to this workspace); click on "Add source folder" and browse to the common\src directory (e.g. "C:\Users\Bojan\workspace\Python\common\src").
import sys
from util.crypto import get_md5

def main():
    print get_md5(r"c:\test\foo.exe")

if __name__ == "__main__":

Links and references:

How to organize a Python Project?
The Python Tutorial: Modules


Python said...

Thanks for this wonderful post.
Python Online Training

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : ( ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;