/usr/local/lib/swipl/library/ext/swipy/janus.pl
All Application Manual Name SummaryHelp

  • swipy
    • janus.pl -- Call Python from Prolog
      • py_version/0
      • py_call/1
      • py_call/2
      • py_call/3
      • py_iter/2
      • py_iter/3
      • py_setattr/3
      • py_is_object/1
      • py_is_dict/1
      • py_free/1
      • py_with_gil/1
      • py_gil_owner/1
      • py_func/3
      • py_func/4
      • py_dot/3
      • py_dot/4
      • values/3
      • keys/2
      • key/2
      • items/2
      • py_shell/0
      • py_pp/1
      • py_pp/2
      • py_pp/3
      • py_object_dir/2
      • py_object_dict/2
      • py_obj_dir/2
      • py_obj_dict/2
      • py_type/2
      • py_isinstance/2
      • py_module_exists/1
      • py_hasattr/2
      • py_import/2
      • py_module/2
      • py_initialize/3
      • py_lib_dirs/1
      • py_add_lib_dir/1
      • py_add_lib_dir/2
 py_import(+Spec, +Options) is det
Import a Python module. Janus imports modules automatically when referred in py_call/2 and related predicates. Importing a module implies the module is loaded using Python's __import__() built-in and added to a table that maps Prolog atoms to imported modules. This predicate explicitly imports a module and allows it to be associated with a different name. This is useful for loading nested modules, i.e., a specific module from a Python package as well as for avoiding conflicts. For example, with the Python selenium package installed, we can do in Python:
>>> from selenium import webdriver
>>> browser = webdriver.Chrome()

Without this predicate, we can do

?- py_call('selenium.webdriver':'Chrome'(), Chrome).

For a single call this is fine, but for making multiple calls it gets cumbersome. With this predicate we can write this.

?- py_import('selenium.webdriver', []).
?- py_call(webdriver:'Chrome'(), Chrome).

By default, the imported module is associated to an atom created from the last segment of the dotted name. Below we use an explicit name.

?- py_import('selenium.webdriver', [as(browser)]).
?- py_call(browser:'Chrome'(), Chrome).
Errors
- permission_error(import_as, py_module, As) if there is already a module associated with As.