Skip to content
AgileLogics
7 min read

Scripting with WolframScript and the Wolfram Engine

Prerequisites: Packages and paclets

The Wolfram Engine plus WolframScript lets you use the language like any scripting language: from the terminal, in cron jobs, or from other programs.

A script with arguments

Save as greet.wls:

#!/usr/bin/env wolframscript
(* greet.wls — prints a greeting *)
args = Rest[$ScriptCommandLine];
name = If[args === {}, "world", First[args]];
Print["Hello, " <> name <> "!"];

Run it:

$ wolframscript -file greet.wls Ada
Hello, Ada!
$ wolframscript -file greet.wls
Hello, world!
  • $ScriptCommandLine is the list of the script's path followed by its arguments — hence Rest. Arguments are strings; use ToExpression or Interpreter["Integer"] to convert.
  • On Linux and macOS, chmod +x greet.wls lets you run ./greet.wls Ada directly, thanks to the first line.
  • Only Print output appears; results of other lines are discarded.
  • Exit[1] ends the script with an exit status, so shell scripts can detect failures.

One-liners

wolframscript -code 'FactorInteger[2^32 + 1]'
{{641, 1}, {6700417, 1}}

That's Euler's famous factorization of the fifth Fermat number.

Calling from Python

The Wolfram Client Library for Python (pip install wolframclient) starts a local engine and exchanges data with it:

# illustrative Python code
from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr

session = WolframLanguageSession()
print(session.evaluate(wlexpr('Integrate[x^2, {x, 0, 3}]')))
print(session.evaluate(wl.Prime(100)))
session.terminate()

This prints 9 and 541. Lists come back as Python sequences and associations as dictionaries. The same library can call APIs you deployed to the Wolfram Cloud.

Calling other languages from Wolfram

(* illustrative: needs Python installed and registered *)
ExternalEvaluate["Python", "sum(range(10))"]

ExternalEvaluate runs Python, JavaScript (Node.js), and other languages and converts results to Wolfram expressions — here 45. RunProcess runs any command-line program.

Other integration points

  • LibraryLink loads C/C++ functions from a shared library into the kernel for speed or to reuse existing code.
  • WSTP (Wolfram Symbolic Transfer Protocol) connects external programs to a kernel.
  • Jupyter — the Wolfram Language for Jupyter kernel runs the engine inside Jupyter notebooks.

Licensing reminder

The free Wolfram Engine for Developers is licensed for development and pre-production use. Running it in production — a public service or a commercial product — needs a production license.