Plugin

JEP – Java Embedded Python

Publication ID: ICY-88973

Short Description

Jep embeds CPython in Java through JNI.

Code on Python through Java !

Team: Bioimage Analysis Unit
Institution: Institut Pasteur
Website: https://research.pasteur.fr/en/team/bioimage-analysis/

Documentation

Some benefits of embedding CPython in a JVM:

  • Using the native Python interpreter may be much faster than alternatives.
  • Python is mature, well-supported, and well documented.
  • Access to high quality Python modules, both native CPython extensions and Python-based.
  • Compilers and assorted Python tools are as mature as the language.
  • Python is an interpreted language, enabling scripting of established Java code without requiring recompilation.
  • Both Java and Python are cross-platform, enabling deployment to different operating systems.

Installation instructions

Simply run pip install jep or download the source and run python setup.py build install. Building and installing require the JDK, Python, and optionally numpy to be installed beforehand.

On your Icy plugin project, add this dependency

<depedency>
    <groupId>org.bioimageanalysis.icy</groupId>
    <artifactId>icy-jep</artifactId>
    <version>4.0.3</version>
</depedency>

Dependencies

  • Python >= 3.5
  • Java >= 1.8
  • NumPy >= 1.7 (optional)

Notable features

  • Interactive Jep console much like Python’s interactive console
  • Supports multiple, simultaneous, mostly sandboxes sub-interpreters or shared interpreters
  • Numpy support for Java primitive arrays

Help

We welcome comments, contributions, bug reports, wiki documentation, etc.

If you need help, please first search for existing solutions online, in the issues, and on the wiki. If you still need help, please open a GitHub issue, and we will try and help you. Please remember to close the issue once it has been resolved.

Jep Team

Citation

Please also cite the Icy software and mention the version of Icy you used (bottom right corner of the GUI or first lines of the Output tab):
de Chaumont, F. et al. (2012) Icy: an open bioimage informatics platform for extended reproducible research, Nature Methods, 9, pp. 690-696
https://icy.bioimageanalysis.org

Author(s)

JEP Team Nate Jensen & Ben Steffensmeier

Plugin conversion Amandine Tournay

Note

Until JEP 4.0.3, for Linux users, you need to set temporarily the environment variable LD_PRELOAD to your libpythonx.xx.so file.
It is due to some difficulties with Java and C projects that dlopen libraries.
It has been fixed for the next version planned for Fall 2022.

Utility functions

// Check if a String is a system path
JepPlugin.getInstance().isPath("<path>");

// Get a list of Conda environments with their full path
PythonUtils.getInstance().detectCondaEnvironments();

// Get a list of only conda environment names
PythonUtils.getInstance().getCondaEnvNames();

// Find the Python execution file by the root directory of Python or check the full path if it is given
PythonUtils.getInstance().findPythonExecutable("<path>", Boolean.TRUE || Boolean.FALSE);

// Retrieve the site-packages directory from the selected Python
PythonUtils.getInstance().setSitePackagesDirectory("<Python path>");

// Retrieve the running JEP file from the site-packages directory of the selected Python
 JepUtils.getInstance().findJepLib("<site-packages path>");

// Instantiate JEP
JepUtils.getInstance().setJepPath("<JEP path>", "<Python root path>");

// Open a Python instance (to use with a try-catch(JepException)
SubInterpreter python = JepUtils.getInstance().openSubPython();
// Same but adding a JEP configuration object to manage better the link between Java and Python (see JavaDoc)
SubInterpreter python2 = JepUtils.getInstance().openSubPython(new JepConfig());

// Running some Python code
python2.exec("x = 5");
python2.runScript("<Python script file path>");
// Prints 5
System.out.println(python2.getValue("x"));
        
// Send data to Python
python2.set("y", 10);
// Prints 10 on the Python output stream
python2.exec("print(y)");
        
// Close a Python instance
JepUtils.getInstance().closeSubPython(python);
// Always close an opened instance !
JepUtils.getInstance().closeSubPython(python2);

Leave a Review

Leave a review
Cancel review
View full changelog
Close changelog

Changelog

  • Version 4.0.3.0 • Released on: 2022-08-03 00:00:00
    Download