I assume you saw this? You do need 9.1.12 due to some new stuff in the Prolog C API that is used by the Python binding
I saw but I didnāt understand. Itās the Prolog release I have to install from source. My apologies
I am not so smart sometimes. Thanx for the directions
If you are using the Macports version, Iām afraid so. Hopefully weāll see 9.1.13 soon.
Improving as a Python package
Iāve pushed several updates to make the interface work better as a Python package that you can install using pip
, given a installation of SWI-Prolog 9.1.12 or later. See Embedding Prolog into Python from the repo README.
Works for me on Windows 11, using the normal Python binary and Microsoft Visual C++ to support pip
in compiling C code.
It probably also works for many configurations on other systems. Please give it a try and report issues. Even better, try and fix it. Figuring out where to find Prolog is all written in Python. Assuming pip knows where to find Python and the C compiler, this should work in pretty much any environment.
Note: This is just some rapid feedback, will see if I can get it working today but if you have a clue that would help, I am listening.
Just updated SWI-Prolog to daily build for Windows
https://www.swi-prolog.org/download/daily/bin/
https://www.swi-prolog.org/download/daily/bin/swipl-w64-2023-08-06.exe
Here is the result
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.12-14-g35822504d)
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: c:/program files/swipl/library/janus.pl:93:
ERROR: c:/program files/swipl/library/janus.pl:93: Initialization goal raised exception:
ERROR: The specified module could not be found.
ERROR: In:
ERROR: [53] throw(error(shared_object(open,'The specified module could not be found.\r\n'),context(...,_624)))
ERROR: [51] <meta call>
ERROR: [50] with_mutex('$foreign',load_foreign_library(foreign(janus),janus,[...])) <foreign>
ERROR: [48] '$run_init_goal'(shlib:load_foreign_library(...,...)) at c:/program files/swipl/boot/init.pl:823
ERROR: [47] catch(system:'$run_init_goal'(...),_766,system:'$initialization_error'(_788,...,...)) at c:/program files/swipl/boot/init.pl:563
ERROR: [46] catch_with_backtrace(system:'$run_init_goal'(...),_824,system:'$initialization_error'(_846,...,...)) at c:/program files/swipl/boot/init.pl:643
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_free/1 is not defined
ERROR: Exported procedure janus:py_iter/3 is not defined
ERROR: Exported procedure janus:py_with_gil/1 is not defined
ERROR: Exported procedure janus:py_call/1 is not defined
ERROR: Exported procedure janus:py_call/2 is not defined
ERROR: Exported procedure janus:py_call/3 is not defined
ERROR: Exported procedure janus:py_iter/2 is not defined
ERROR: [Thread 1]: exception handler failed to define janus:py_call/2
ERROR: Unknown procedure: janus:py_call/2
ERROR: In:
ERROR: [11] janus:py_call(sys:version,_11474)
ERROR: [10] janus:py_version at c:/program files/swipl/library/janus.pl:112
ERROR: [9] toplevel_call(user:user:py_version) at c:/program files/swipl/boot/toplevel.pl:1173
Exception: (11) janus:py_call(sys:version, _9754) ?
?- pwd.
% c:/users/Groot/documents/prolog/
true.
Other info
From Windows command line
C:\Users\Groot>where swipl
C:\Program Files\swipl\bin\swipl.exe
C:\Users\Groot>python -V
Python 3.10.0
C:\Users\Groot>where python
C:\Users\Groot\AppData\Local\Programs\Python\Python310\python.exe
C:\Users\Groot\AppData\Local\Microsoft\WindowsApps\python.exe
SWI-Prolog is started from the Windows taskbar by clicking an icon (shortcut - *.lnk) , the shortcut uses this command line
āC:\Program Files\swipl\bin\swipl-win.exeā --win_app
The Windows install wizard noted the Python interface
The file janus.pl
line 93 shows
:- use_foreign_library(foreign(janus), [visibility(global)]).
At this point not sure if the DLL was created automatically or I need to create it but will run that down; just wanted to give you some rapid feedback.
Thanks. This is an open issue. It works (should work ā¦) fine, but requires Python 3.11. As janus.dll directly depends on libpython3.11, it doesnāt work with python 3.10. There might be a way around. In the Windows version we find next to python311.dll
a file python3.dll
. I guess that exists in all python 3 distributions. Iāll check whether that can work.
The question however was whether the python package works. So could you try to clone GitHub - SWI-Prolog/packages-swipy: Python interface for SWI-Prolog, go in there and run
pip install .
After that you should be able to do import janus
in Python. Note that you can run
>>> janus.prolog()
to get the interactive Prolog toplevel
This should work on any Windows system with a properly installed CPython and pip and SWI-Prolog 9.1.12 or later with or without janus
installed. But, there are many variations, so most likely we will need some more conditions in setup.py
Thanks, my bad should have checked.
Upgrading now.
From earlier post
Did find janus.dll
installed with SWI-Prolog into C:\Program Files\swipl\bin
, that was a relief as I was fearing I might have to build it.
Just recently I realised this would be awesome for me.
But, FreeBSD, which doesnāt really work so well with swipl in the past.
However, I want it soā¦
Step 1 - cmake still doesnāt work so start with
export LDFLAGS=-lstdthreads
Step 2 - start building again
cmake -DCMAKE_INSTALL_PREFIX=$HOME/sys -G Ninja ā¦
ninja
Hmm, lots of errors in nonblockio.c. Much digging - Aha header file looks more like Apple.
Step 3 - hack nonblockio.c
/* Everybody seems to defined
struct in6_addr
in a different way ā¦
*/#ifndef s6_addr16
#if defined(s6_words)
#define s6_addr16 s6_words
#elif defined(APPLE)
#define s6_addr16 __u6_addr.__u6_addr16
#else
#define s6_addr16 __u6_addr.__u6_addr16
#endif
#endif
I donāt know if thereās a flag for FreeBSD so this was a bit of a hack.
Step 4 - start building again
Progress but fails with various messages like:
ld: error: relocation R_X86_64_PC32 cannot be used against symbol ā_PyRuntimeā; recompile with -fPIC
defined in /home/eddie/sys/lib/libpython3.10.a(pylifecycle.o)
referenced by pycore_pystate.h:62 (./Include/internal/pycore_pystate.h:62)
abstract.o:(PyMapping_Size) in archive /home/eddie/sys/lib/libpython3.10.a
Do some google - Sigh, it requires a version of Python compiled with
āenable-shared
At this point any FreeBSD user not using their own built Python may be home free.
For the rest:
Step 5 - rebuild Python with --enable-shared
Step 6 - start building again
The only caveat for the intrepid FreeBSDer at this point is to make sure the new python3 is on your path, including for new shells because cmake seems to be invoking new shells (or something).
Done. At least I can invoke py_shell, more elaborate tests for later.
Thanks for the update!
I think this is something that must be raised either with FreeBSD or kitware (the cmake developers). SWI-Prolog simply uses CMakeās find_package(Threads)
. That should do the trick
Thanks. FreeBSD defines __FreeBSD__
. Updated the condition, so this should no longer be needed.
Yes. You need a relocatable (PIC) object for embedding on POSIX like systems. Youād expect that to be the default these days. In the i386 days that was not always the case because relocatable code was slower. With x64/amd64 and I guess any modern CPU this problem is gone, so there is very little reason not to build a relocatable object.
Apart from linking and version problems, I think the interface is pretty much cross-platform. Run
ctest -R janus
or
src/swipl ../packages/swipy/test_janus.pl
?- test_janus.
There are only 47 tests so far ā¦
Pushed changes to use python3.dll
. This turns out a dll that relays the stable Python3 API to the specific python3xx.dll. Required a few patches, avoiding more recent Python API calls if we link against python3.dll
. Tomorrows nightly build should run with a much wider range of Python3 binaries on Windows. Possibly still not with the early 3.x versions.
% All 47 tests passed in 0.310 seconds (0.309 cpu)
I downloaded the Prolog release from source, I updated the cmake to the latest release, I followed these instructions
Building from source
The typical sequence to build SWI-Prolog and install in /usr/local is as follows:
cd swipl-devel
mkdir build
cd build
cmake -G Ninja ā¦
ninja
ctest -j 8
ninja install
still I get the following message, when I enter the āmake -G Ninja ā¦ā command:
marco@MacBookoBachini ~ % cd /usr/local
marco@MacBookoBachini local % ls
Caskroom PortDetect.log include remotedesktop var
Cellar bin join_air_data sbin
Frameworks etc lib share
Homebrew hw_mp_userdata opt swipl-9.1.12
marco@MacBookoBachini local % cd swipl-9.1.12
marco@MacBookoBachini swipl-9.1.12 % sudo mkdir build
marco@MacBookoBachini swipl-9.1.12 % cd build
marco@MacBookoBachini build % cmake -G Ninja ..
CMake Error: Unable to (re)create the private pkgRedirects directory:
/usr/local/swipl-9.1.12/build/CMakeFiles/pkgRedirects
I think Iāll wait for the 9.1.13 release
For Windows 10 (Update)
Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.12-14-g35822504d)
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.
% Janus embeds Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
true.
What I will work on next, actually using the package.
Details for others needing to install for Windows.
Installed SWI-Prolog using build of the day.
https://www.swi-prolog.org/download/daily/bin/swipl-w64-2023-08-06.exe
Installed Python from
using
python-3.11.4-amd64.exe
For first dialog of Python install wizard checked box for Add python.exe to PATH
Windows environment variable for path
for users, not system variables, contains
C:\Users\Groot\AppData\Local\Programs\Python\Python311\Scripts
C:\Users\Groot\AppData\Local\Programs\Python\Python311\
To check that python
works from command line.
C:\Users\Groot>where python
C:\Users\Groot\AppData\Local\Programs\Python\Python311\python.exe
C:\Users\Groot\AppData\Local\Microsoft\WindowsApps\python.exe
C:\Users\Groot>python -V
Python 3.11.4
C:\Users\Groot>python3 -V
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Using Windows Powershell
PS C:\Users\Groot> get-command python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.11.41... C:\Users\Groot\AppData\Local\Programs\Python\Python311\python.exe
Possibly more goes wrong, but this surely does. If you create build
as super user you cannot write there as normal user, so you cannot build there. Normally you do the download and build as normal user in a place you can write. Only the last step, if you want to install in a global place, you run sudo ninja install
.
There is another thing I wanted to ask, maybe you know how I can solve the issue: I changed the zprofile of the Terminal and added the PATH to the latest Prolog release . Nevertheless if I tip swipl from the command line it always enters the āoldā version 9.0.4, as if it didnāt āseeā the newly inserted PATH. But if I tip echo $PATH the path variable is there. I donāt know. At the moment I am stuck with these two things
So maybe I should try to build at a different location
I built on my home directory I think it worked
ā¦
One has to have a GitHub account in order to clone this janus repository stuff? So I think I was only halfway successful. But at least the build process worked, the new Prolog release works from the terminal now and I can enter in an interactive python shell from prolog, not the other way around I think. But for the moment I consider it as a step forward
You do not need a github account. Did you install from the git repo or from the tarball?
Ok, I made it I may not always be the smartest guy around but also perseverance is important