Skillversitas

Packages

A package is a directory of modules that forms a namespace, marked by the __init__.py file. Jython follows the standard Python package layout exactly, so a Jython package can mix your own modules and even re-export Java classes under a clean namespace.

Package Layout

mypkg/
  __init__.py
  mathutils.py
  textutils.py
  sub/
    __init__.py
    deep.py
# mypkg/__init__.py
from .mathutils import is_prime, gcd
from . import textutils

__version__ = "0.1.0"
import mypkg
print(mypkg.is_prime(13))
print(mypkg.textutils.shout("hi"))   # if textutils defines shout

from mypkg.sub.deep import something
import mypkg.sub.deep as d

Relative Imports

# inside mypkg/mathutils.py
from . import textutils        # sibling module
from .sub.deep import f        # child package module (if applicable)

A leading dot means "relative to this package". Use them inside a package; use absolute imports (import mypkg.mathutils) from outside.

Packages That Expose Java

A neat Jython pattern: a package that re-exports curated Java classes so downstream code never writes java.util directly:

# mypkg/containers.py
from java.util import ArrayList, HashMap, HashSet

__all__ = ["ArrayList", "HashMap", "HashSet"]
from mypkg.containers import ArrayList
lst = ArrayList()
lst.add("a")
print(lst)

Using Your Package

# simplest: put the package dir on PYTHONPATH
export PYTHONPATH="$PWD/src:$PYTHONPATH"
jython -c "import mypkg; print(mypkg.__version__)"

For distribution, a setup.py/pyproject.toml with the package name works as in Python; Jython respects the standard packaging metadata.

Summary

Jython packages are standard Python packages: a directory with __init__.py, subpackages, and relative imports (from . import x). The Jython-specific payoff is that packages can re-export Java classes (from java.util import ArrayList) to give your project a clean, Java-free public API.