A possible solution for using the janus interface on MacOS with docker

 Experimenting with the Janus module on docker

Janus is a new development that comes with swi-prolog version 9: 

janus documentation

Having attempted to run the example in the above link,

I tried the following steps to install and use the spacy python package:


pip install spacy  

docker run -it --rm --network host -v $(pwd):/opt/janus\
                                   -v /path/to/python3.11/site-packages:/opt/python\
                                   swipl
?- consult('/opt/janus/test_janus.pl').
?- py_add_lib_dir('/opt/python').                                   
?- noun("This is a sentence.", Noun).
ERROR: Python 'ImportError':
ERROR:   Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
ERROR: Python stack:
ERROR:   File "/opt/python/spacy/__init__.py", line 6, in module
ERROR:     from .errors import setup_default_warnings
ERROR:   File "/opt/python/spacy/errors.py", line 3, in module
ERROR:     from .compat import Literal
ERROR:   File "/opt/python/spacy/compat.py", line 4, in module
ERROR:     from thinc.util import copy_array
ERROR:   File "/opt/python/thinc/__init__.py", line 2, in module
ERROR:     import numpy
ERROR: 
ERROR: In:
ERROR:   [25] janus:py_call(spacy:load(en_core_web_sm),_318)

Solution for importing the numpy package

the numpy package is installed on MacOS ...

however the python version from the swipl docker image is based on debian linux:

docker-swipl

hence it comes as no surprise that it couldn't get loaded !

 

A first attempt at installing numpy in the swipl container


docker run -it --rm --network host -v $(pwd):/opt/janus swipl
?- process_create(path(pip), ['install', 'numpy'], []).
ERROR: Could not find executable file "pip" in $PATH

Creating a new docker image

For solving the "pip" not avaible issue, I decided to create my own docker image based on the docker-swipl github

incodame/swipy

Line 23:

I changed it to use the python3-full to enable the creation of a virtual environment for python:

PEP-668

Line 25:

this line adds the pip module

Using the new docker image


docker run -it --rm --network host -v $(pwd):/opt/janus incodame/swipy
?- consult('/opt/janus/test_janus.pl').
true.

?- process_create(path(python3), ['-m', 'venv', '/opt/python_venv'], []).
true.

?- process_create('/opt/python_venv/bin/pip', ['install', 'numpy'], []).
Collecting numpy
  Downloading numpy-2.2.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (14.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.3/14.3 MB 5.8 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-2.2.2
true.

?- py_add_lib_dir('/opt/python_venv/lib/python3.11/site-packages').
true.

After that container configuration is performed, one can use the

py_import

janus call to use the python libraries newly installed in the virtual environment.

Comments

Popular posts from this blog

Meaningful Xml Diffs with SWI-Prolog

Prolog DCG "distilled" - Part 3 - Capturing and assembling textual elements from rules