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:

AddTwo::usage = "AddTwo[a, b] returns a+b";
AddTwo[a_, b_] := a + b;

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?


2 Answers 2


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.

    wl.SetDirectory('<dir of .m file>')

Evaluate function from your package.

res = wolfSession.evaluate(

Terminate the Wolfram session.


Hope this helps.

  • $\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$ Commented Apr 3, 2020 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
    Commented Apr 3, 2020 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$ Commented Apr 3, 2020 at 20:30
  • 1
    $\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$ Commented Apr 4, 2020 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
    Commented Jun 26, 2021 at 3:47

If you use wl.Get['packagefilename.m'] it should work.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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