Any folder containing one or more Jython modules is recognized as a package. However, it must have a special file called __init__.py, which provides the index of functions to be used.
Let us now understand, how to create and import package.
Step 1 − Create a folder called package1, then create and save the following g modules in it.
#fact.py def factorial(n): f = 1 for x in range(1,n+1): f = f*x return f
#sum.py def add(x,y): s = x+y return s
#mult.py def multiply(x,y): s = x*y return s
Step 2 − In the package1 folder create and save the __init__.py file with the following content.
#__init__.py from fact import factorial from sum import add from mult import multiply
Step 3 − Create the following Jython script outside the package1 folder as test.py.
# Import your Package. import package1 f = package1.factorial(5) print "factorial = ",f s = package1.add(10,20) print "addition = ",s m = package1.multiply(10,20) print "multiplication = ",m
Step 4 − Execute test.py from Jython prompt. The following output will be obtained.
factorial = 120 addition = 30 multiplication = 200