Full access to the Wolfram Language from Python. See how to set up a connection, plus a few methods and examples to do computations. The client library is fully open sourced as the WolframClientForPython git repository.
So today we’re happy to formally announce a new and often-requested connection that allows you to call the Wolfram Language directly and efficiently from Python: the Wolfram Client Library for Python. And, even better, this client library is fully open source as the WolframClientForPython git repository under the MIT License, so you can clone it and use it any way you see fit.
“The Wolfram Client Library for Python is fully open source.”
It’s Easy The Wolfram Client Library makes it easy to integrate the large collection of Wolfram Language algorithms as well as the Wolfram Knowledgebase directly into any Python code that you already have. This saves you considerable time and effort when developing new code. In this post, we’ll first show you how to set up a connection from Python to the Wolfram Language. Next, we’ll explore a few methods and examples you can use to do a computation in the Wolfram Language and then call it for use in your Python session. For a complete introductory tutorial and full reference documentation, visit the documentation home page for the Wolfram Client Library for Python.
Evaluate Locally… Let’s start with a simple example, which computes the mean and standard deviation of one million numbers drawn from a normal distribution. This example shows how to call a Wolfram Language function from Python and compares the results from Python and the Wolfram Language to show that they are numerically close to one another.
Statistical Data Analysis
First, to connect to the Wolfram Language, you need to create a new session with the Wolfram Engine:
〉
from wolframclient.evaluation import WolframLanguageSession session = WolframLanguageSession()
To call Wolfram Language functions, you need to import the `wl` factory:
〉
from wolframclient.language import wl
Now you can evaluate any Wolfram Language code. Set the Python variable sample to a list of one million random numbers drawn from the normal distribution, with a mean of 0 and a standard deviation of 1:
Again run the following code in Python to verify that you get a similar result:
〉
stdev(sample) 1.0014296230797068
It’s good to see that these results are comparable. Now you know how to call some simple Wolfram Language functions from Python. Let’s continue with a more exciting example.
moons = session.evaluate(wl.WolframAlpha('moons of Saturn', 'Result'))
The WolframAlpha function is one of the high-level functions in the Wolfram Language that interacts with the Wolfram|Alpha servers via a web API. You can use this API directly from Python, but doing it by calling the WolframAlpha function is much more powerful and convenient because you can access all the data framework functions from the Wolfram Language directly. Let’s take a look at what the Python variable moons contains:
〉
moons EntityClass['PlanetaryMoon', 'SaturnMoon']
The output here is the Python representation of a Wolfram Language expression, which can be reused in any subsequent evaluation. For example, if you want to get the list of Saturn’s first four moons (by proximity) explicitly, you can do this:
This is all pretty cool. Let’s take a look at another example, using the Wolfram Language’s built-in image processing and machine learning functions.
Image Processing and Machine Learning
First, let’s switch over to another mode to do evaluations directly in the Wolfram Language. So far you’ve used the `wl` factory to build up Wolfram Language expressions in Python. But you can also evaluate Python strings containing Wolfram Language code, and sometimes this is easier to read:
〉
from wolframclient.language import wlexpr
For example, you can evaluate 1+1 in the Wolfram Language by sending it as a string:
〉
session.evaluate('1+1') 2
Using this method, you can write a small snippet of Wolfram Language code that takes an image and uses the built-in face-detection algorithm to find the location of a face in an image. Here, the image we’re using is the famous painting titled Girl with a Pearl Earring by the Dutch painter Johannes Vermeer (but it works on almost any image with recognizable faces). Because the Python terminal interface does not support the display of images, we’ll need use a Jupyter notebook instead, together with the Python Image Library (PIL) package, to help with displaying the result:
Quite easy and powerful. But what if you don’t have a local installation of the Wolfram Engine, and want to use the Wolfram Client Library for Python? You can still use the Wolfram Language directly in the Wolfram Cloud.
To the Cloud The Wolfram Cloud provides easy access to the Wolfram Language without needing to install it locally. The Wolfram Cloud provides various services, including a notebook web interface for Wolfram Language programming as well as the capability to deploy arbitrary Wolfram Language web APIs.
Here you’ll make use of the latter, deploying a Wolfram Language web API. This particular API accepts the names of two countries (country1 and country2), finds the capital city for each country and then computes the distance between them (in kilometers):
After the deployment of this API, you can start a new Wolfram Language session, but this time you connect to the Wolfram Cloud instead of the local desktop engine:
〉
from wolframclient.evaluation import WolframCloudSession cloud = WolframCloudSession()
To call the API, you have to provide the username (user1) and the API endpoint (api/public/capital_distance). With that information, you can call the cloud...
〉
api = ('user1', 'api/public/capital_distance') result = cloud.call(api, {'country1': 'Netherlands', 'country2': 'Spain'})
Available Now Install the latest version of the Wolfram Client Library for Python with pip:
$ pip install wolframclient
It requires Python 3.5.3 (or above) and Wolfram Language 11.3 (or above). Check out the full documentation on the Wolfram Client Library for Python. The entire source code is hosted in the WolframClientForPython repository on the Wolfram Research GitHub site. And if you see a way to improve it, you can help us make it better by contributing pull requests to this repository.
We’re very excited about this release and hope you find it useful. Let us know what you think in the comments section or on Wolfram Community, and we’ll do our best to respond to questions.
Cite this as: Dorian Birraux, Arnoud Buzing, "Announcing the Wolfram Client Library for Python" from the Notebook Archive (2019), https://notebookarchive.org/2019-05-ddtni45