Janus and Conda

Hi all,
I’m trying to install swipl in a conda environment so that it uses the python executable and packages from the environment.
No luck yet.
I tried the suggestions here
https://www.swi-prolog.org/pldoc/man?section=janus-venv
and I tried configuring with

cmake -DCMAKE_INSTALL_PREFIX=$HOME -DINSTALL_DOCUMENTATION=OFF -DPython_ROOT_DIR=~/.conda/envs/pytorch/bin -G Ninja ..

and with

cmake -DCMAKE_INSTALL_PREFIX=$HOME -DINSTALL_DOCUMENTATION=OFF -DPython_FIND_VIRTUALENV=FIRST -G Ninja ..

When I look at CMakeCache.txt it seems it has correctly picked up the environment:

//Path to a program.
PROG_PYTHON:FILEPATH=/hpc/home/rzf/.conda/envs/pytorch/bin/python3

//Path to a program.
PROTOC:FILEPATH=PROTOC-NOTFOUND

//No help, variable specified on the command line.
Python_ROOT_DIR:UNINITIALIZED=~/.conda/envs/pytorch/bin

but when I run swipl I get:

(pytorch) [rzf@hnode01 nations]$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.3.11-8-gfb487feae)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- py_version.
% Interactive session; added `.` to Python `sys.path`
% Janus 1.4.0 embeds Python 3.9.16 (main, Dec  8 2022, 00:00:00)
[GCC 11.3.1 20221121 (Red Hat 11.3.1-4)]
true.

while

(pytorch) [rzf@hnode01 nations]$ python
Python 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:50:21)
[GCC 12.3.0] on linux

What really matters is not where it finds python, but where it finds the library for an embedded Python system. E.g.

> grep libpython CMakeCache.txt 
FIND_PACKAGE_MESSAGE_DETAILS_Python:INTERNAL=[/bin/python3.12][/usr/include/python3.12][/usr/lib64/libpython3.12.so][cfound components: Interpreter Development Development.Module Development.Embed ][v3.12.5(3.6)]
_Python_LIBRARY_RELEASE:INTERNAL=/usr/lib64/libpython3.12.so

Note that Python and the embedding library are for most Linux distros distinct packages. I do not know about conan.

But then, I do not know (doubt it) that it will set RUNPATH (or RPATH) of the Janus interface. It surely does not when using the above standard location. Doesn’t conan set LD_LIBRARY_PATH?

No, conda does not set it.

I made another experiment by using a different major version of Python for the conda environment, so that the difference between the two environments is clearer. I have Python 3.9 outside conda and 3.11 in the conda environment torch.

Now I see in CMakeCache.txt:

//Details about finding Python
FIND_PACKAGE_MESSAGE_DETAILS_Python:INTERNAL=[/hpc/home/rzf/.conda/envs/torch/bin/python3][/hpc/home/rzf/.conda/envs/torch/include/python3.11][/hpc/home/rzf/.conda/envs/torch/lib/libpython3.11.so][cfound components: Interpreter Development Development.Module Development.Embed ][v3.11.10(3.6)]

So the installation has correctly got the Python library.

However, swipl cannot find it

(torch) [rzf@hnode01 buildconda4]$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.3.11-8-gfb487feae)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- py_version.
ERROR: /hpc/home/rzf/lib/swipl/library/ext/swipy/janus.pl:116:
ERROR:    /hpc/home/rzf/lib/swipl/library/ext/swipy/janus.pl:116: Initialization goal raised exception:
ERROR:    libpython3.11.so.1.0: cannot open shared object file: No such file or directory
ERROR:    In:
ERROR:      [55] throw(error(shared_object(open,'libpython3.11.so.1.0: cannot open shared object file: No such file or directory'),context(...,_664)))
ERROR:      [53] <meta call>
ERROR:      [52] with_mutex('$foreign',load_foreign_library(foreign(janus),janus,[...])) <foreign>
ERROR:      [50] '$run_init_goal'(shlib:load_foreign_library(...,...)) at /hpc/home/rzf/lib/swipl/boot/init.pl:826
ERROR:      [49] catch(system:'$run_init_goal'(...),_806,system:'$initialization_error'(_828,...,...)) at /hpc/home/rzf/lib/swipl/boot/init.pl:565
ERROR:      [48] catch_with_backtrace(system:'$run_init_goal'(...),_864,system:'$initialization_error'(_886,...,...)) at /hpc/home/rzf/lib/swipl/boot/init.pl:645
ERROR:
ERROR:    Note: some frames are missing due to last-call optimization.
ERROR:    Re-run your program in debug mode (:- debug.) to get more detail.
ERROR: Exported procedure janus:py_is_object/1 is not defined
ERROR: Exported procedure janus:py_iter/2 is not defined
ERROR: Exported procedure janus:py_gil_owner/1 is not defined
ERROR: Exported procedure janus:py_free/1 is not defined
ERROR: Exported procedure janus:py_call/3 is not defined
ERROR: Exported procedure janus:py_with_gil/1 is not defined
ERROR: Exported procedure janus:py_setattr/3 is not defined
ERROR: Exported procedure janus:py_call/1 is not defined
ERROR: Exported procedure janus:py_iter/3 is not defined
ERROR: Exported procedure janus:py_call/2 is not defined
ERROR: [Thread 1]: exception handler failed to define janus:py_call/2
ERROR: Unknown procedure: janus:py_call/2
ERROR:   However, there are definitions for:
ERROR:         janus:px_call/4
ERROR:         janus:py_call/4
ERROR:
ERROR: In:
ERROR:   [13] janus:py_call(sys:version,_13856)
ERROR:   [12] janus:py_version at /hpc/home/rzf/lib/swipl/library/ext/swipy/janus.pl:161
ERROR:   [11] toplevel_call(user:user:py_version) at /hpc/home/rzf/lib/swipl/boot/toplevel.pl:1318
   Exception: (13) janus:py_call(sys:version, _12136) ? creep
?-

% halt

If I use

export LD_LIBRARY_PATH=/hpc/home/rzf/.conda/envs/torch/lib/

then it works

(torch) [rzf@hnode01 buildconda4]$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.3.11-8-gfb487feae)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- py_version.
% Interactive session; added `.` to Python `sys.path`
% Janus 1.4.0 embeds Python 3.11.10 | packaged by conda-forge | (main, Sep 10 2024, 11:19:15) [GCC 13.3.0]
true.

?-

So, possibly we need an option to use RUNPATH on janus.so if the Python lib is not in a default location? At least, you have a work-around :slight_smile:

I’ve had similar problems when other libraries weren’t in the default location. So, a generic way of adding libraries (rpath? LIBRARY_PATH?) to the executable could be good. (I think I first ran into this when I built gcc in ~/.local)

Should it be an installation option or a runtime option?

It can only be a cmake option. At runtime you have LD_LIBRARY_PATH and various tools to edit the runtime path of binaries. But, this is all very platform specific :frowning:

I’d first have to check what cmake offers out of the box. I know it has several options to control RUNPATH on ELF systems.