How to select the venv when using Janus?

Hi all,

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.

Have you tried:

  • Add the path of your python3.8 excutable to the start of the PATH environment variable
  • Add a symlink from python to your python3.8executable, in the same directory

In BASH, the symlink can be created by changing to the directory containing the python3.8 executable, then running:

ln -s python3.8 python

I think the path is already in the PATH environment variable.

(venv) myname@LAPTOP-G7GSPOTS:~/myjob$ echo $PATH
/home/myname/myjob/venv/bin:......

And I tried your second advice. But it doesn’t seem to work :pleading_face:

(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.

Has PATH been exported? This is crucial for swipl to inherit the changed PATH. Do so in BASH with:

export PATH

Hold on, that’s the wrong directory. Show the contents of ~/myjob/venv/bin/

Using ls -l, rather than just ls, so we can distinguish files and directories and executables.

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 :folded_hands:

Why? Python 3.8.x is end-of-life according to Download Python | Python.org

Python 3.8 was in Ubuntu 2020: python3.8 package : Ubuntu

Ubuntu 2020 ended its standard support in May-2025: Ubuntu release cycle | Ubuntu

You should most probably try the second option if this is too difficult.

1 Like

Because in my project, I need a package which is easier to install in python3.8-3.10. I spent quite long time install it in python3.12 but failed.

I think that is the best advice. See janus-swi

Thank you so much. I tried and find it really easy to get it work using the second option.

1 Like