1   Notes On Jython

author:Thava Alagu thavamuni@gmail.com
Version:0.1
Date:2016 October 07
status:Draft

1.1   Overview

Jython combines the power of Python’s ease of programming with Java’s rich library implementation. It follow’s Python’s syntax where Java library API is available.

What Does Jython Do Well?

Prototyping
Java investigation
    >>> from java.util import Date
    >>> d = Date()
    >>> print d
    Sat Jan 08 16:26:16 CST 2005
    >>> from java.util import Random
    >>> print dir(Random)
    ['__init__', 'nextBoolean', 'nextBytes', 'nextDouble', 'nextFloat',
    'nextGaussian', 'nextInt', 'nextLong', 'seed', 'setSeed']
    >>>

Making bean properties accessible
    >>> print Date().time
    1105500911121

Glues together libraries already written in Java
Excellent embedded scripting language

Differences - Python & Jython

Python                                   Jython

C
Multi-platform                           100% Java
Compiles to .pyc                         Any JVM (currently 1.1+)
Extend with C                            Compiles to .class
GIL                                      Extend with Java
Python Garbage Collection                Truly multi-threaded
                                         Java garbage collection

1.2   Controlling Module Search Path

The sys module contains the path object which specifies the list of module search directories. To examine it’s value, we must import the sys module first:

>> import sys
>> from pprint import pprint
>> pprint(sys.path)

 ['',
  '/home/user/myenv/bin',
  '/home/user/myenv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg',
  '/home/user/myenv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
  '/home/user/myenv/lib/python2.7',
  '/home/user/myenv/lib/python2.7/plat-linux2',
  '/home/user/myenv/lib/python2.7/lib-tk',
  '/home/user/myenv/lib/python2.7/lib-old',
  '/home/user/myenv/lib/python2.7/lib-dynload',
  '/usr/lib/python2.7',
  '/usr/lib/python2.7/plat-linux2',
  '/usr/lib/python2.7/lib-tk',
  '/home/user/myenv/local/lib/python2.7/site-packages',
  '/home/user/myenv/local/lib/python2.7/site-packages/IPython/extensions']

The module search path (i.e. sys.path) includes certain platform dependent default directories. In addition, you can control the search path by one of the following methods:

  • Set JYTHONPATH environment variable to include additional directories. They will be prefixed into your module search path. (PYTHONPATH for cpython)

  • The program can dynamically modify the module search path. It can do the following first:

    >>> import sys
    >>> sys.path.insert(0, '/path/to/my/module')
    
  • If you are running virtualenv, then you can choose to place your module in the local site-packages directory without affecting other installations.

Table Of Contents

Previous topic

Notes On Node.JS

Next topic

1   Linked in Capabilities

This Page