package pyml

  1. Overview
  2. Docs

OCaml Interface for Python.

Call initialize () first.

val initialize : ?library_name:string -> ?interpreter:string -> ?version:int -> ?minor:int -> ?verbose:bool -> ?debug_build:bool -> ?python_sigint:bool -> unit -> unit

initialize ~interpreter ~version ~minor ~verbose ~debug_build () finds and loads the Python library. This function should be called before any other functions, except if explicitely mentioned. If library_name is given, it is used as the path for the library to be loaded: in this case, version parameters are ignored. If library_name is not given, the library is searched as described below. version should specify the major version number of Python (2 or 3). minor should specify the minor version number. If no version number is given, the version of Python is determined by the output of the shell command python --version. If an interpreter executable name is given, this executable is used in place of python in the previous command line. The library is searched by using pkg-config if available, by considering system paths, and in the directory ../lib relatively to the directory where the python executable is. If the library has been statically linked with the executable, it will be used. When verbose is true (default: false), library filenames that are tried to be loaded are printed on standard error. debug_build specifies whether the Python library is a debug build: if the argument is left unspecified, debug build is detected automatically. If python_sigint is true (default: false), the function let pythonlib take handle on sigint, preventing programs from being interrupted by Ctrl+C. When python_sigint is false (the default), the previous signal behavior of sigint is restored after the library has been loaded (so, Ctrl+C will still interrupt the program, unless this behavior was changed elsewhere).

val finalize : unit -> unit

finalize () unloads the library. No other functions except initialize () should be called afterwards.

val on_finalize : (unit -> unit) -> unit

on_finalize f registers f () to be executed when finalize is executed.

val is_initialized : unit -> bool

is_initialized () returns true if the library is initialized (initialize () has been called and finalize () has not been called afterwards).

val is_debug_build : unit -> bool

is_debug_build () returns true if the library is a debug build.

val get_library_filename : unit -> string option

get_library_filename () returns Some filename where filename is the path to the Python library that has been loaded, or None if no Python library has been loaded (for example, if the library has been statically linked with the executable).

val version : unit -> string

version () returns the version of the Python library. E.g. "3.5.1".

val version_major : unit -> int

version_major () returns the major number (the first component) of the version of the Python library, either 2 or 3.

val version_minor : unit -> int

version_minor () returns the minor number (the second component) of the version of the Python library.

val version_pair : unit -> int * int

version_pair () returns the major and the minor numbers of the version of the Python library.

type compare = Pytypes.compare =
  1. | LT
  2. | LE
  3. | EQ
  4. | NE
  5. | GT
  6. | GE
type 'a file = 'a Pytypes.file =
  1. | Filename of string
  2. | Channel of 'a

Either a filename or a channel. Channels suppose that the same C runtime has been used to compile both the Python library and the OCaml runtime. Warning: using channels is unsafe if runtimes differ (can lead to segmentation fault).

val check_error : unit -> unit
module Object : sig ... end

General functions to handle Python values

exception E of Object.t * Object.t

E (errtype, errvalue) is a Python error. errtype is the type of the exception. errvalue is the value.

val null : Object.t

The value NULL of the C Python API. null is useful for calling directly the functions of Pywrappers module. The value should not appear when using the functions of the Py module. This value is guaranteed to be the unique value associated to NULL.

val is_null : Object.t -> bool

Py.is_null v is true if and only if v is NULL. Since Py.none is guaranteed to be the unique value associated to NULL, Py.is_null v is equivalent to v == Py.null.

val check_not_null : Object.t -> Object.t

check_not_null v checks that v is not null and returns v. Raises the current Python error as exception otherwise.

val none : Object.t

The value None of Python. This value is guaranteed to be the unique value associated to None.

val is_none : Object.t -> bool

Py.is_none v is true if and only if v is None. Since Py.none is guaranteed to be the unique value associated to None, Py.is_none v is equivalent to v == Py.none.

val set_program_name : string -> unit

Sets the program name (by default, Sys.argv.(0)). The function can be called before initialize () and the value is preserved from one initialization to the other.

val set_python_home : string -> unit

Sets the path of the Python home. The function can be called before initialize () and the value is preserved from one initialization to the other.

val add_python_path : string -> unit

Adds a path to Python search path. The function can be called before initialize () and the value is preserved from one initialization to the other.

val get_program_name : unit -> string

Gets the program name (by default, Sys.argv.(0)). The function can be called before initialize ().

val get_python_home : unit -> string

Gets the path of the Python home. The function can be called before initialize ().

val get_program_full_path : unit -> string
val get_prefix : unit -> string

Wrapper for Py_GetPrefix.

val get_exec_prefix : unit -> string

Wrapper for Py_GetExecPrefix.

val get_path : unit -> string

Wrapper for Py_GetPath.

val get_version : unit -> string

Wrapper for Py_GetVersion.

val get_platform : unit -> string

Wrapper for Py_GetPlatform.

Wrapper for Py_GetCopyright.

val get_compiler : unit -> string

Wrapper for Py_GetCompiler.

val get_build_info : unit -> string

Wrapper for Py_GetBuildInfo.

module Bool : sig ... end

Interface for Python values of type Bool.

module Callable : sig ... end

Interface for Python values of type Callable.

module Capsule : sig ... end

Embedding of OCaml values in Python.

module Class : sig ... end

Defining a new class type

module Long : sig ... end

Interface for Python values of type Long.

module Int : sig ... end

Interface for Python values of type Int if Python 2, Long if Python 3.

module Dict : sig ... end

Interface for Python values of type Dict.

module Set : sig ... end

Interface for Python values of type Set.

module Err : sig ... end
module Traceback : sig ... end
exception Err of Err.t * string

Represents an exception to be set with Err.set_error in a callback.

exception Err_with_traceback of Err.t * string * Traceback.t

Represents an exception with traceback information to be set with Err.restore.

module Eval : sig ... end
module Float : sig ... end

Interface for Python values of type Float.

type optimize =
  1. | Default
  2. | Debug
  3. | Normal
  4. | RemoveDocstrings
val int_of_optimize : optimize -> int
module Import : sig ... end

Importing Modules

val import : string -> Object.t

Equivalent to Import.import_module.

val import_opt : string -> Object.t option
module Iter : sig ... end

Interface for Python values of type Iter.

module List : sig ... end

Interface for Python values of type List.

module Mapping : sig ... end

Interface for Python values with a Mapping interface.

module Method : sig ... end

Interface for Python values of type Method.

type input = Pytypes.input =
  1. | Single
  2. | File
  3. | Eval
val string_of_input : input -> string
module Module : sig ... end

Interface for Python values of type Module.

module Number : sig ... end

Interface for Python values of type Number.

module Run : sig ... end

Interface for Python values of type Run.

module Sequence : sig ... end

Interface for Python values with a Sequence interface.

type byteorder =
  1. | LittleEndian
  2. | BigEndian
module String : sig ... end

Interface for Python values of type String, Bytes and Unicode.

module Bytes : sig ... end

Interface for Python values of type Bytes. With Python 2, aliases for String.

module Tuple : sig ... end

Interface for Python values of type Tuple.

module Type : sig ... end

Introspection of Python types

module Marshal : sig ... end
module Array : sig ... end
module Gil : sig ... end
val set_argv : string array -> unit

set_argv argv set Python's sys.argv.

val last_value : unit -> Object.t

last_value () returns the last value that was computed in the toplevel. We have Py.last_value = Py.Module.find (Py.Module.builtins ()) "_".

val exception_printer : exn -> string option

This printer pretty-prints E (ty, value) exceptions. It is automatically registered to Printexc.register_printer.

val compile : source:string -> filename:string -> ?dont_inherit:bool -> ?optimize:[ `Default | `Debug | `Normal | `RemoveDocstrings ] -> [ `Exec | `Eval | `Single ] -> Object.t

Old interface for Py.Module.compile.

OCaml

Innovation. Community. Security.