Janus and Python venv

Most people in the python world use environments for their apps, something like

$ python -m venv venv
$ . venv/bin/activate  # notice the dot which sources the script
(venv)
$ pip install mido
[...]

I did a quick try and environments don’t seem to work in Janus, is there a way to tell it to use the current active environment?

Thanks. My quick try just works fine. What is goind wrong?

$ 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
>>> 

[Moved topic]

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. :frowning:

Some links and remarks

Seems we must

  • set sys.prefix to $VIRTUAL_ENV
  • 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?

Pushed d08f3497bf3e1bff90d077fd5c89c0e06e8f4cd7

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

1 Like

I installed a python package in the venv, and I was able to import the package from janus py_shell, so it looks like it is working well. Thanks!