Sunday, July 13, 2025

Getting Wolfram Mathematica to work from Python, Jax, and Pytorch or whatever

I have the desktop version of Wolfram 14.2 personal edition that comes with 4 kernel instances in the license. I wanted to connect to it with Python and Jax and when I followed the docs I couldn't actually get it to work with no real error and when I ran WolframKernel it was saying it required a license. When I would try to generate a password manually WolframKernel would reject it. I didn't want to use my 2nd key for this instance(and don't even know if it would have actually worked LoL), but nothing I did would stop me from getting licensing prompts coming from WolframKernel or wolframscript.

The answer in my case was that I had a hung WolframKernel. After exiting everything I knew of including the GUI and all terminal sessions I had been using to access Wolfram I still found this:

ps aux | grep WolframKernel | grep -v grep
user    <pid 1>  0.8  0.8 9018716 133060 ?      Sl   18:22   0:16 /usr/local/Wolfram/Wolfram/14.2/SystemFiles/Java/Linux-x86-64/bin/java -classpath /usr/local/Wolfram/Wolfram/14.2/SystemFiles/Links/JLink/JLink.jar com.wolfram.jlink.util.LinkSnooper -noinit -kernelname /usr/local/Wolfram/Wolfram/14.2/Executables/WolframKernel -mathlink -linkprotocol SharedMemory -linkconnect -linkname 8hctc_shm
user    <pid 2>  0.4  0.0      0     0 ?        Z    18:22   0:07 [WolframKernel] <defunct>

I couldn't just kill -15 or SIGTERM it either, it required a good old fashioned SIGKILL:

kill -9 <pid 1>

After this I was able to open WolframKernel directly by command and cleanly exit it. I was also able to call it from python code following the directions given. This will put it in a virtual environment also for a more stable containerized like solution. More can be found here

To setup

python -m venv wolfram
cd wolfram
source bin/activate #requires simply typing deactivate to exit once in the venv

if You have the 14.2 desktop like me and want the newest client

pip install /usr/local/Wolfram/Wolfram/14.2/SystemFiles/Components/WolframClientForPython

If You have an older version of the desktop You will need to find those files for Your version and could probably do something like:

pip install $(dirname $(readlink $(which WolframKernel)))/../SystemFiles/Components/WolframClientForPython

If You do not have wolfram desktop software that supports this, then You will need to get one of those free licenses as stated above and do

pip install wolframclient

#To run code I was able to test with the following

python3
from wolframclient.language import wl
from wolframclient.evaluation import WolframLanguageSession #this is deprecated in python after 3.12 and will likely start failing soon
session = WolframLanguageSession('/usr/local/Wolfram/Wolfram/14.2/Executables/WolframKernel') #Seems to require the whole path and not the /usr/local/bin link
range5=session.evaluate(wl.Range(5))