Skip to content, Skip to search


Jython Scripting

1,926 bytes added, 02:28, 29 September 2016
Self written Jython packages for ImageJ: Adding content to this section.
== Self written Jython packages for ImageJ ==
On the way to perfectly organize Jython code, [ packages] are the next step. In Jython, folders that contain modules are made packages by adding the file <code></code>. This file can be empty. An folder structure can look like this:
-- myPackage
-- myPackage2
There are two packages and one module. The first package contains three modules and the second package contains two modules. We can import the modules on different ways:
<source lang='python'>
# Import the single module using the default name:
import myModule
# Import mathTools from the first package
import myPackage.mathTools
# Use a function from the imported module
# Import mathTools from the second package
from myPackage2 import mathTools
# Use a function from the imported module without prefixing the package
# Import customFilters from the first package and rename it
from myPackage import customFilters as filters
# Use a function from
# Importing all module from a package
from myPackage2 import *
# The next line will fail
The reason for the last import to fail is the empty <code></code>. We have to define which modules of the package are imported when using <code>import *</code>. This is done by setting the variable <code>__all__</code> at <code></code>. For <code>myPackage2</code> this line of code is needed:
<source lang='python'>
__all__ = ["mathTools", "stackProcessing"]
Besides setting this variable, the file can contain normal Jython code that is executed on import.
== Using maven to build packages ==
== References ==
<references />