Class Jep
- java.lang.Object
-
- jep.Jep
-
- All Implemented Interfaces:
java.lang.AutoCloseable,Interpreter
- Direct Known Subclasses:
SharedInterpreter,SubInterpreter
public class Jep extends java.lang.Object implements Interpreter
As of Jep 3.9, you should strive to instantiate either a SubInterpreter or a SharedInterpreter instance and for interacting with the interpreter use the interface Interpreter. If you previously used Jep instances, use SubInterpreter instances to retain the same behavior.Embeds CPython in Java. Each Jep provides access to a Python interpreter and maintains an independent global namespace for Python variables. Values can be passed from Java to Python using the various set() methods. Various methods, such as
eval(String)andinvoke(String, Object...)can be used to execute Python code. Python variables can be accessed usinggetValue(String).In general, methods called on a Jep instance must be called from the same thread that created the instance. To maintain stability, avoid having two Jep instances running on the same thread at the same time. Instead provide different threads or close() one before instantiating another on the same thread. Jep instances should always be closed when no longer needed to prevent memory leaks.
-
-
Constructor Summary
Constructors Constructor Description Jep()Deprecated.Deprecated in 3.9.Jep(boolean interactive)Deprecated.Please useJep(JepConfig)instead.Jep(boolean interactive, java.lang.String includePath)Deprecated.Please useJep(JepConfig)instead.Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl)Deprecated.Please useJep(JepConfig)instead.Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl, ClassEnquirer ce)Deprecated.Please useJep(JepConfig)instead.Jep(JepConfig config)Deprecated.Deprecated in 3.9.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Shuts down the Python interpreter.booleaneval(java.lang.String str)Evaluate Python statements.voidexec(java.lang.String str)Execute an arbitrary number of Python statements in this interpreter.java.lang.ObjectgetValue(java.lang.String str)Retrieves a value from this Python interpreter.<T> TgetValue(java.lang.String str, java.lang.Class<T> clazz)LikeInterpreter.getValue(String)but allows specifying the return type.byte[]getValue_bytearray(java.lang.String str)Deprecated.use Python 3 bytes object instead andgetValue(String,Class)with byte[].class Retrieves a Python string object as a Java byte[].java.lang.Objectinvoke(java.lang.String name, java.lang.Object... args)Invokes a Python function.java.lang.Objectinvoke(java.lang.String name, java.lang.Object[] args, java.util.Map<java.lang.String,java.lang.Object> kwargs)Invokes a Python function.java.lang.Objectinvoke(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> kwargs)Invokes a Python function.booleanisInteractive()Deprecated.This may be removed in a future version of Jep.voidisValidThread()Deprecated.For internal usage only.voidrunScript(java.lang.String script)Runs a Python script.voidrunScript(java.lang.String script, java.lang.ClassLoader cl)Deprecated.This may be removed in a future version of Jep, as Jep does not fully support changing the ClassLoader after construction.voidset(java.lang.String name, boolean v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, boolean[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, byte b)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, byte[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, char v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, char[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, double v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, double[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, float v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, float[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, int v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, int[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, long v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, long[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, short v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, short[] v)Deprecated.Useset(String, Object)instead.voidset(java.lang.String name, java.lang.Object v)Sets the Java Object into the interpreter's global scope with the specified variable name.voidset(java.lang.String name, java.lang.String v)Deprecated.Useset(String, Object)instead.voidsetClassLoader(java.lang.ClassLoader cl)Deprecated.This may be removed in a future version of Jep.static voidsetInitParams(PyConfig config)Deprecated.Please useMainInterpreter.setInitParams(PyConfig)instead.voidsetInteractive(boolean v)Deprecated.This may be removed in a future version of Jep.static voidsetSharedModulesArgv(java.lang.String... argv)Deprecated.Please useMainInterpreter.setSharedModulesArgv(String...)instead.
-
-
-
Constructor Detail
-
Jep
@Deprecated public Jep() throws JepExceptionDeprecated.Deprecated in 3.9. Use SubInterpreter or SharedInterpreter instead. If you used Jep objects in previous releases, use SubIntepreter for the same behavior.Creates a newJepinstance and its associated interpreter.- Throws:
JepException- if an error occurs
-
Jep
@Deprecated public Jep(boolean interactive) throws JepExceptionDeprecated.Please useJep(JepConfig)instead.Creates a newJepinstance and its associated sub-interpreter.- Parameters:
interactive- whethereval(String)should support the slower behavior of potentially waiting for multiple statements- Throws:
JepException- if an error occurs
-
Jep
@Deprecated public Jep(boolean interactive, java.lang.String includePath) throws JepExceptionDeprecated.Please useJep(JepConfig)instead.Creates a newJepinstance and its associated sub-interpreter.- Parameters:
interactive- whethereval(String)should support the slower behavior of potentially waiting for multiple statementsincludePath- a path of directories separated by File.pathSeparator that will be appended to the sub-intepreter'ssys.path- Throws:
JepException- if an error occurs
-
Jep
@Deprecated public Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl) throws JepExceptionDeprecated.Please useJep(JepConfig)instead.Creates a newJepinstance and its associated sub-interpreter.- Parameters:
interactive- whethereval(String)should support the slower behavior of potentially waiting for multiple statementsincludePath- a path of directories separated by File.pathSeparator that will be appended to the sub-intepreter'ssys.pathcl- the ClassLoader to use when importing Java classes from Python- Throws:
JepException- if an error occurs
-
Jep
@Deprecated public Jep(boolean interactive, java.lang.String includePath, java.lang.ClassLoader cl, ClassEnquirer ce) throws JepExceptionDeprecated.Please useJep(JepConfig)instead.Creates a newJepinstance and its associated sub-interpreter.- Parameters:
interactive- whethereval(String)should support the slower behavior of potentially waiting for multiple statementsincludePath- a path of directories separated by File.pathSeparator that will be appended to the sub-intepreter'ssys.pathcl- the ClassLoader to use when importing Java classes from Pythonce- aClassEnquirerto determine which imports are Python vs Java, or null for the defaultClassList- Throws:
JepException- if an error occurs
-
Jep
@Deprecated public Jep(JepConfig config) throws JepException
Deprecated.Deprecated in 3.9. Use SubInterpreter or SharedInterpreter instead. If you used Jep objects in previous releases, use SubIntepreter for the same behavior.Creates a newJepinstance and its associated interpreter.- Parameters:
config- the configuration for the Jep instance- Throws:
JepException- if an error occurs
-
-
Method Detail
-
setInitParams
@Deprecated public static void setInitParams(PyConfig config) throws JepException
Deprecated.Please useMainInterpreter.setInitParams(PyConfig)instead.Sets interpreter settings for the top Python interpreter. This method must be called before the first Jep instance is created in the process.- Parameters:
config- the python configuration to use.- Throws:
JepException- if an error occurs- Since:
- 3.6
-
setSharedModulesArgv
@Deprecated public static void setSharedModulesArgv(java.lang.String... argv) throws JepExceptionDeprecated.Please useMainInterpreter.setSharedModulesArgv(String...)instead.Sets the sys.argv values on the top interpreter. This is a workaround for issues with shared modules and should be considered experimental.- Parameters:
argv- the arguments to be set on Python's sys.argv for the top/main interpreter- Throws:
JepException- if an error occurs- Since:
- 3.7
-
isValidThread
@Deprecated public void isValidThread() throws JepExceptionDeprecated.For internal usage only. Internal OnlyChecks if the current thread is valid for the method call. All calls must check the thread.- Throws:
JepException- if an error occurs
-
runScript
public void runScript(java.lang.String script) throws JepExceptionDescription copied from interface:InterpreterRuns a Python script.- Specified by:
runScriptin interfaceInterpreter- Parameters:
script- aStringabsolute path to script file.- Throws:
JepException- if an error occurs
-
runScript
@Deprecated public void runScript(java.lang.String script, java.lang.ClassLoader cl) throws JepExceptionDeprecated.This may be removed in a future version of Jep, as Jep does not fully support changing the ClassLoader after construction.Runs a Python script.- Parameters:
script- aStringabsolute path to script file.cl- aClassLoadervalue, may be null.- Throws:
JepException- if an error occurs
-
invoke
public java.lang.Object invoke(java.lang.String name, java.lang.Object... args) throws JepExceptionDescription copied from interface:InterpreterInvokes a Python function.- Specified by:
invokein interfaceInterpreter- Parameters:
name- a Python function name in globals dict or the name of a global object and method using dot notationargs- args to pass to the function in order- Returns:
- an
Objectvalue - Throws:
JepException- if an error occurs
-
invoke
public java.lang.Object invoke(java.lang.String name, java.util.Map<java.lang.String,java.lang.Object> kwargs) throws JepExceptionDescription copied from interface:InterpreterInvokes a Python function.- Specified by:
invokein interfaceInterpreter- Parameters:
name- a Python function name in globals dict or the name of a global object and method using dot notationkwargs- a Map of keyword args- Returns:
- an
Objectvalue - Throws:
JepException- if an error occurs
-
invoke
public java.lang.Object invoke(java.lang.String name, java.lang.Object[] args, java.util.Map<java.lang.String,java.lang.Object> kwargs) throws JepExceptionDescription copied from interface:InterpreterInvokes a Python function.- Specified by:
invokein interfaceInterpreter- Parameters:
name- a Python function name in globals dict or the name of a global object and method using dot notationargs- args to pass to the function in orderkwargs- a Map of keyword args- Returns:
- an
Objectvalue - Throws:
JepException- if an error occurs
-
eval
public boolean eval(java.lang.String str) throws JepExceptionDescription copied from interface:InterpreterEvaluate Python statements.
In interactive mode, Jep may not immediately execute the given lines of code. In that case, eval() returns false and the statement is stored and is appended to the next incoming string.
If you're running an unknown number of statements, finish with
eval(null)to flush the statement buffer.Interactive mode is slower than a straight eval call since it has to compile the code strings to detect the end of the block. Non-interactive mode is faster, but code blocks must be complete. For example:
interactive mode == false
jep.eval("if(Test):\n print('Hello world')");interactive mode == true
jep.eval("if(Test):"); jep.eval(" print('Hello world')"); jep.eval(null);Also, Python does not readily return object values from eval(). Use
Interpreter.getValue(String)instead.Note: Interactive mode will be removed in a future release. This method may still be used for executing individual statements. See console.py for an example of how to interactively execute Python using the builtin compile() and exec() functions.
- Specified by:
evalin interfaceInterpreter- Parameters:
str- aStringstatement to eval- Returns:
- true if statement complete and was executed.
- Throws:
JepException- if an error occurs
-
exec
public void exec(java.lang.String str) throws JepExceptionDescription copied from interface:InterpreterExecute an arbitrary number of Python statements in this interpreter. Similar to the Python builtin exec function.- Specified by:
execin interfaceInterpreter- Parameters:
str- Python code to exececute- Throws:
JepException- if an error occurs
-
getValue
public java.lang.Object getValue(java.lang.String str) throws JepExceptionDescription copied from interface:InterpreterRetrieves a value from this Python interpreter. Supports retrieving:
- Java objects
- Python None (null)
- Python strings
- Python True and False
- Python numbers
- Python lists
- Python tuples
- Python dictionaries
For Python containers, such as lists and dictionaries, getValue will recursively move through the container and convert each item. If the type of the value retrieved is not supported, Jep will fall back to returning a String representation of the object. This fallback behavior will probably change in the future and should not be relied upon.
- Specified by:
getValuein interfaceInterpreter- Parameters:
str- the name of the Python variable to get from the interpreter's global scope- Returns:
- an
Objectvalue - Throws:
JepException- if an error occurs
-
getValue
public <T> T getValue(java.lang.String str, java.lang.Class<T> clazz) throws JepExceptionDescription copied from interface:InterpreterLikeInterpreter.getValue(String)but allows specifying the return type. If Jep cannot convert the variable to the specified type then a JepException is thrown. This can be used to safely ensure that the return value is an expected type. The following table describes what conversions are currently possible.The valid classes for Python to Java conversions Python Class Java Classes Notes str/unicode String,CharacterCharacter conversion will fail if the str is longer than 1. bool Booleanint/long Long,Integer,Short,ByteConversion fails if the number is outside the valid range for the Java type float Double,Floatlist, tuple List, arrayWhen a tuple is converted to a List it is unmodifiable. dict Mapfunction, method Any FunctionalInterface Buffer Protocol array This includes Python classes such as bytes, bytearray and array.array numpy.ndarray NDArrayOnly if Jep was built with numpy support numpy.float64 Double,Floatnumpy.float32 Float,Doublenumpy.int64 Long,Integer,Short,ByteConversion fails if the number is outside the valid range for the Java type numpy.int32 Integer,Long,Short,ByteConversion fails if the number is outside the valid range for the Java type numpy.int16 Short,Integer,Long,ByteConversion fails if the number is outside the valid range for the Java type numpy.int8 Byte.Short,Integer,LongNoneType Any(null) Jep objects such as PyJObjects and jarrays will be returned if the Java type of the wrapped object is compatible. Anything else String,PyObjectString conversion will likely be removed in future versions of Jep so it is unsafe to depend on this behavior. - Specified by:
getValuein interfaceInterpreter- Type Parameters:
T- the generic type of the return type- Parameters:
str- the name of the Python variable to get from the interpreter's global scopeclazz- the Java class of the return type.- Returns:
- a Java version of the variable
- Throws:
JepException- if an error occurs
-
getValue_bytearray
@Deprecated public byte[] getValue_bytearray(java.lang.String str) throws JepExceptionDeprecated.use Python 3 bytes object instead andgetValue(String,Class)with byte[].class Retrieves a Python string object as a Java byte[].- Parameters:
str- the name of the Python variable to get from the sub-interpreter's global scope- Returns:
- an
Objectarray - Throws:
JepException- if an error occurs
-
setClassLoader
@Deprecated public void setClassLoader(java.lang.ClassLoader cl)
Deprecated.This may be removed in a future version of Jep. Jep does not fully support changing the ClassLoader after construction.Sets the default classloader.- Parameters:
cl- aClassLoadervalue
-
setInteractive
@Deprecated public void setInteractive(boolean v)
Deprecated.This may be removed in a future version of Jep.Changes behavior ofeval(String). Interactive mode can wait for further Python statements to be evaled, while non-interactive mode can only execute complete Python statements.- Parameters:
v- if the sub-interpreter should run in interactive mode
-
isInteractive
@Deprecated public boolean isInteractive()
Deprecated.This may be removed in a future version of Jep.Gets whether or not this sub-interpreter is interactive.- Returns:
- whether or not the sub-interpreter is interactive
-
set
public void set(java.lang.String name, java.lang.Object v) throws JepExceptionDescription copied from interface:InterpreterSets the Java Object into the interpreter's global scope with the specified variable name.- Specified by:
setin interfaceInterpreter- Parameters:
name- the Python name for the variablev- anObjectvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, java.lang.String v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java String into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- aStringvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, boolean v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java boolean into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- abooleanvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, int v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java int into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- anintvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, short v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java short into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- anintvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, char[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java char[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- achar[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, char v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java char into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- acharvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, byte b) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java byte into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variableb- abytevalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, long v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java long into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- alongvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, double v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java double into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- adoublevalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, float v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java float into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- afloatvalue- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, boolean[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java boolean[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- aboolean[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, int[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java int[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- anint[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, short[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java short[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- ashort[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, byte[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java byte[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- abyte[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, long[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java long[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- along[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, double[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java double[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- adouble[]value- Throws:
JepException- if an error occurs
-
set
@Deprecated public void set(java.lang.String name, float[] v) throws JepExceptionDeprecated.Useset(String, Object)instead.Sets the Java float[] into the sub-interpreter's global scope with the specified variable name.- Parameters:
name- the Python name for the variablev- afloat[]value- Throws:
JepException- if an error occurs
-
close
public void close() throws JepExceptionShuts down the Python interpreter. Make sure you call this to prevent memory leaks.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceInterpreter- Throws:
JepException
-
-