$ python -c "import sys; print('in venv') if sys.prefix != sys.base_prefix else print('not in venv')"
not in venv
$ . venv/bin/activate
(venv)
$ python -c "import sys; print('in venv') if sys.prefix != sys.base_prefix else print('not in venv')"
in venv
(venv)
$ swipl
2 ?- py_shell .
Python 3.11.5 (main, Sep 2 2023, 14:16:33) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import sys; print('in venv') if sys.prefix != sys.base_prefix else print('not in venv')
not in venv
>>>
I see. I was building the Janus interface as Python package in the venv. That works fine. Here the problem is that the embedded Python does not listen to to the virtualenv. Does anyone know how to handle this? The virtualenv seems to make two changes to the environment:
set VIRTUAL_ENV
modify PATH to add $VIRTUAL_ENV/bin
I found quite a couple of suggestions on how to handle this, but mostly contradicting, not working, etc.
Avoid preset site-packages. Docs says this is done using -I, but this only part of the story: using Python inside a venv also removes the system wide local packages and -I does not; it only removes the user one. It seems venv removes all directories with site-packages and dist-packages
Add the site-packages from $VIRTUAL_ENV. Only, this is (for me) in $VIRTUAL_ENV/lib/python3.10/site-packages. Form where do I get the 3.10? Is this always the major.minor version?
. venv/bin/activate
swipl
?- py_version.
% Janus embeds Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
% Janus: using venv from '/home/janw/venv'
true.
Updates sys.prefix and sys.path as described above. Works using Python 3.10 on Linux. No clue whether there is something OS/Python version specific. Not unlikely, so please report problems.
How to make janus work with conda virtual env? I set export VIRTUAL_ENV=~/miniconda3 and janus still doesn’t recognize it. I also have the conda env activated, so when I run which python3 outside of swipl shell I get ~/miniconda3/bin/python3.
A bit more information is required to say anything sensible. I think you are talking about Janus as SWI-Prolog library (as opposed to Janus as Python package for embedding Prolog into Python), right?
It seems we are also talking about some non-Windows OS. Which?
The SWI-Prolog build creates janus.so/?? as a SWI-Prolog plugin that is loaded by janus.pl That shared object binds to libpython. This should be the libpython that belongs to the Python you want to run. Now, it all depends on the OS, how Prolog is build/installed and the Python versions.
Yeah, I was referring to the SWI-Prolog library and my setup is calling python code from prolog. I’m on Ubuntu 22.04.
The issue was janus library didn’t use the conda environment that was active and was using the python binary under /usr/bin. Anyways, I was able to resolve the issue using the suggestions in this thread. Essentially, building SWI-Prolog from the source and setting -DPython_ROOT_DIR when running cmake.
As a side-effect, building SWI-Prolog from the source resulted in losing REPL functionalities such as going through previously run history via Up/Down keys and tab completion.
You probably missed some dependencies. See Prerequisites for Debian based systems (Ubuntu, Mint, ...) for the complete list. In this case you probably missed libedit-dev, but you may be missing more functionality. Install all of them, then in the build dir run
cmake .
ninja
ninja install
The cmake . will also tell you about missing dependencies. Building SWI-Prolog with missing dependencies just drops some functionality. Only libz is a hard requirement.