When I was using Janus to embed python into prolog, it always select the python in /use/bin/. Its version is 3.12.3. But I need python in the version 3.8.20. So I create a virtual environment with python3.8.20. But when I open swipl in the virtual environment, Janus still select the python in /use/bin/, which is in the wrong version.
myname@LAPTOP-G7GSPOTS:~/myjob$ python --version
Python 3.12.3
myname@LAPTOP-G7GSPOTS:~/myjob$ source venv/bin/activate
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ python --version
Python 3.8.20
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ python -c "import sys; print('in venv') if sys.prefix != sys.base_prefix else print('not in venv')"
in venv
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.2.9)
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`
Warning: Janus: venv dirrectory '/home/ssj20021212/myjob/venv' does not contain "/home/myname/myjob/venv/lib/python3.12/site-packages"
% Janus 1.5.1 embeds Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
% Janus: using venv from '/home/myname/myjob/venv'
true.
It seems that Janus is trying to search for the python in the venv, but it specified the python version as 3.12 while searching. So it misses the python3.8 in the venv, which really exists.
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ cd /home/myname/myjob/venv/lib/
(venv) myname@LAPTOP-G7GSPOTS:~/myjob/venv/lib$ ls
python3.8
(venv) myname@LAPTOP-G7GSPOTS:~/myjob/venv/lib$ cd /home/myname/myjob/venv/lib/python3.8/
(venv) myname@LAPTOP-G7GSPOTS:~/myjob/venv/lib/python3.8$ ls
site-packages
I wonder how to solve the problem, which has confused me for quite a while.
And I tried your second advice. But it doesn’t seem to work
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ ls -l python
lrwxrwxrwx 1 myname myname 9 Sep 9 22:27 python -> python3.8
(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.2.9)
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`
Warning: Janus: venv dirrectory '/home/ssj20021212/minihack/minihack' does not contain "/home/myname/myjob/venv/lib/python3.12/site-packages"
% Janus 1.5.1 embeds Python 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
% Janus: using venv from '/home/myname/myjob/venv'
true.
The problem is that Janus loads Python as a library (.so file). So, to use a particular venv with a specific Python version you need to build Janus (in your case the version that embeds Python into Prolog) with the exact Python library version of the Python in the venv. (Hope I’m a bit clear).
How to do that depends largely on the OS. One option is probably to make sure the Python development system (C header and python.so) are installed in the venv and then build Prolog with Janus inside the venv. The other is to ensure the correct Python and its development system are installed, build Prolog using these and then create the venv. If you have multiple Python environments installed, you can ask the SWI-Prolog configuration to pick a particular one using e.g. (this is taken from the Macports build)
cmake ... -DCPYTHON_VERSION="3.13\;EXACT" ...
The alternative is to install the Python Janus package in the venv. Note that the Janus interface is identical regardless of who embeds who once it is loaded. Here is a session running the Prolog REPL started from Python:
> python
Python 3.13.7 (main, Aug 14 2025, 00:00:00) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import janus_swi as janus
>>> janus.prolog()
1 ?- pwd.
% /home/jan/src/macports-ports/
true.
2 ?- emacs.
true.
My OS is Ubuntu 24.04.3 LTS.
And I’m a beginner in this area and not very familiar with the process. So would you mind providing more detailed instructions on how to carry out the first option?
Thank you in advance