7
$\begingroup$

This question is about calling a Mathematica Package (.m file) from Python employing the "Wolfram Client Library for Python" (https://github.com/WolframResearch/WolframClientForPython).

For concreteness, consider the following Mathematica Package:

BeginPackage["basicPackage`"]
AddTwo::usage = "AddTwo[a, b] returns a+b";
Begin["Private`"]
AddTwo[a_, b_] := a + b;
End[]
EndPackage[]

Aim: call AddTwo from Python using the Wolfram Client Library for Python. If this is not currently supported add this feature to the library.

The documentation for the library is located at: https://reference.wolfram.com/language/WolframClientForPython/index.html.

A similar question, asked before the Wolfram Client Library for Python was released, is: How to run a Mathematica package (.m) from python?

$\endgroup$
8
$\begingroup$

You need to evaluate in a WolframLanguageSession after you setup the Wolfram Client For Python. Loading a package adds its context to $ContectPath and contexts can be specified with the wl object (see Advanced Usages) in the guide.

Load the objects in Python and start the Wolfram kernel session.

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr
wolfSession = WolframLanguageSession()

Load your package in the Wolfram session.

wolfSession.evaluate(
    wl.SetDirectory('<dir of .m file>')
    wl.Needs('basicPackage`')
    wl.ResetDirectory()
)

Evaluate function from your package.

res = wolfSession.evaluate(
        wl.basicPackage.AddTwo(1,2)
    )
print(res)

Terminate the Wolfram session.

wolfSession.terminate()

Hope this helps.

$\endgroup$
5
  • $\begingroup$ For some reason, wl.SetDirectory and wl.ResetDirectory produce issues: wl.Needs("basicPackage`") ^ SyntaxError: invalid syntax. If these lines are commented out (and the .m file is in the same location as the suggested code), it works. $\endgroup$ Apr 3 '20 at 19:37
  • $\begingroup$ @user10181864 Could be the string you have in wl.SetDirectory. Be sure it is a valid with the `` character property escaped in Python syntax. $\endgroup$
    – Edmund
    Apr 3 '20 at 19:50
  • $\begingroup$ This does not seem to be the case. Tried all sorts of possible `` characters. In any case, the question has been answered. $\endgroup$ Apr 3 '20 at 20:30
  • $\begingroup$ +1. Just wanted to point out, that ResetDirectory[] is not guaranteed to reset the directory back to what it was, since SetDirectory[] could've been called multiple times by the code in the package being loaded. In general, the SetDirectory - ResetDirectory is a failed concept, which I would avoid using. $\endgroup$ Apr 4 '20 at 0:04
  • $\begingroup$ Anybody run into an issue where the package takes a long time to load? Got it working with the AddTwo example, but one of my lengthier packages with package dependencies (including Notation`) just seems to hang.. Might post a separate question $\endgroup$
    – Sterling
    Jun 26 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.