public class PyLib extends Object
When the PyLib
class is loaded, it reads its configuration from a Java properties file called .jpy
which must exist in the current user's home directory. The configuration file has been written to this location
by installing the Python jpy module using python3 setup.py install --user
on Unix
and python setup.py install
) on Windows.
Currently, the following properties are recognised in the .jpy
file:
python.lib
- the Python shared library (usually required on Unix only)jpy.lib
- the jpy shared library path for Python (Unix: jpy*.so
, Windows: jpy*.pyd
)
jpy API clients should first call isPythonRunning()
in order to check if a Python interpreter is already available.
If not, startPython(String...)
must be called before any other jpy API is used.
Important note for developers: If you change the signature of any of the native PyLib
methods,
you must first run javah
on the compiled class, and then adapt src/main/c/jni/org_jpy_PyLib.c
.
Modifier and Type | Class and Description |
---|---|
static class |
PyLib.CallableKind
The kind of callable Python objects.
|
static class |
PyLib.Diag
Controls output of diagnostic information for debugging.
|
Modifier and Type | Method and Description |
---|---|
static void |
assertPythonRuns()
Throws a runtime exception if Python interpreter is not running.
|
static <T> T |
ensureGil(java.util.function.Supplier<T> runnable) |
static int |
execScript(String script)
Deprecated.
|
static PyObject |
getCurrentGlobals()
Return a dictionary of the global variables in the current execution frame, or NULL if no
frame is currently executing.
|
static PyObject |
getCurrentLocals()
Return a dictionary of the local variables in the current execution frame, or NULL if no
frame is currently executing.
|
static String |
getDllFilePath() |
static PyObject |
getMainGlobals() |
static String |
getPythonVersion() |
static boolean |
hasGil() |
static boolean |
isPythonRunning() |
static boolean |
setProgramName(String programName)
Useful for virtual environments, helps in setting sys.prefix/exec_prefix.
|
static boolean |
setPythonHome(String pythonHome)
Does the equivalent of setting the PYTHONHOME environment variable.
|
static void |
startPython(int flags,
String... extraPaths)
Starts the Python interpreter.
|
static void |
startPython(String... extraPaths)
Delegates to
startPython(int, String...) with flags = Diag.F_OFF . |
static void |
stopPython()
Stops the Python interpreter.
|
public static String getDllFilePath()
public static void assertPythonRuns()
startPython(String...)
yet.public static boolean isPythonRunning()
true
if the Python interpreter is running and the the 'jpy' module has been loaded.public static void startPython(String... extraPaths)
startPython(int, String...)
with flags = Diag.F_OFF
.public static void startPython(int flags, String... extraPaths)
flags
- If non-zero, is passed to PyLib.Diag.setFlags(int)
before python is startedextraPaths
- List of paths that will be prepended to Python's 'sys.path'.RuntimeException
- if Python could not be started or if the 'jpy' extension module could not be loaded.public static boolean setPythonHome(String pythonHome)
startPython()
.
Supported for Python 2.7, and Python 3.5 or higherpythonHome
- Path to Python Home (must be less than 256 characters!)public static boolean setProgramName(String programName)
startPython()
.programName
- Path to Python executable (must be less than 256 characters!)public static String getPythonVersion()
public static void stopPython()
startPython(java.lang.String...)
currently causes a fatal error in the the Java Runtime Environment originating from
the Python interpreter (function Py_Finalize
in standard CPython implementation).
There is currently no workaround for that problem other than not restarting the Python interpreter from
your code.
For more information refer to https://github.com/bcdev/jpy/issues/70@Deprecated public static int execScript(String script)
public static PyObject getMainGlobals()
public static PyObject getCurrentGlobals()
public static PyObject getCurrentLocals()
public static boolean hasGil()
public static <T> T ensureGil(java.util.function.Supplier<T> runnable)
Copyright © 2014–2022 Brockmann Consult GmbH. All rights reserved.