I have a DynamicModule that requires loading of Mathematica packages. The usual methods do not seem to apply (Needs["MathematicaPackage`"]], etc) I've tried

Dynamic[Initialization :> (Needs["Package`"])];

with only partial success.

I'm sure this is done regularly, but cannot find any MMA documentation to that effect.
What's the best way to load Mathematica packages in Dynamic or DynamicModule and Manipulate?

  • $\begingroup$ Can you expand a bit on what “with only partial success” means? What happened, what is your issue? $\endgroup$
    – F'x
    Apr 14, 2012 at 14:57
  • $\begingroup$ Certainly! Initial results provided package errors using Needs, and it was easy to see the package was not used in the module. Now the errors are related to LibraryFunction::cfta: Argument {{bla,bla,bla},<<44>> at position 2 should be a rank 2 tensor of machine-size real numbers. >> and only a single package loading error followed by the usual list errors related to the package functions that did not load. Thanks for you help! $\endgroup$ Apr 14, 2012 at 15:03
  • 1
    $\begingroup$ Possibly related: mathematica.stackexchange.com/questions/595/… $\endgroup$ Apr 14, 2012 at 17:36
  • $\begingroup$ @LeonidShifrin Not exactly, but thank's for the suggestion. Just wrestling with some of the newer Dynamic functionality and breaking things nicely by trial and error. ;) Thanks again! $\endgroup$ Apr 15, 2012 at 19:43

1 Answer 1


While Initialization is a useful option, it is only evaluated when the body of the DynamicModule is displayed on screen. See some explanation on the evaluation sequence here.

With DynamicModule

I've constructed a TestPackage` for this case, it contains the globally exported variable $Test, with a value of 123. The following example shows that the package is not loaded (note that you have to clear dynamic content and variables (e.g. restart kernel) for each example below to start from a clear state of memory):

DynamicModule[{}, $Test, Initialization :> (Needs["TestPackage`"])]


It doesn't work with an explicit TestPackage`$Test call either indicating that $Test (and thus the body) is evaluated before the Initialization code. This is somewhat contraintuitive, as one expects the initialization code to be evaluated before anything else. I should note here that the package is loaded, it is just loaded too late. Any later cells calling for $Test return the correct value though. One way to overcome this in the given setup is to use the explicit name of $Test in a dynamic output, which updates the unrecognized $Test output when the package is finally loaded during evaluation:

DynamicModule[{}, Dynamic[TestPackage`$Test], Initialization :> Needs["TestPackage`"])]


The second example is even more strange. While the package loading code is included in the body, $Test is still not recognized:

DynamicModule[{}, Needs["TestPackage`"]; $Test]

Mathematica graphics

Using the explicit name of the variable helps here, indicating that parsing (done before evaluation) causes $Test to be a local variable that is not recognized as TestPackage`$Test:

DynamicModule[{}, Needs["TestPackage`"]; TestPackage`$Test]


Of course the easiest way is:

DynamicModule[{}, $Test]



And the Wolfram way is (see this conference material):

"Can be solved by two Needs[] statements...one to fulfill Shift+Enter evaluation and one to fulfill Dynamic evaluation. Note that this is only an issue because of $ContextPath. Any other initialization code could have safely appeared one time inside of Initialization."

DynamicModule[{}, Dynamic[$Test], Initialization :> Needs["TestPackage`"])]


With Manipulate

Following the Wolfram way, one needs the outer package loading, but in Manipulate, SaveDefinitions works correctly grabbing the definition of $Test from the package:

Manipulate[$Test, {$Test, None}, SaveDefinitions -> True]


With Dynamic

According to the above examples, with Dynamic (each cell should be evaluated in a fresh kernel):

Dynamic[$Test, Initialization :> (Needs["TestPackage`"])]


Dynamic[TestPackage`$Test, Initialization :> (Needs["TestPackage`"])]


Dynamic[Needs["TestPackage`"]; $Test]


Dynamic[Needs["TestPackage`"]; TestPackage`$Test]


  • $\begingroup$ Thanks very much @István Zachar! This is an excellent and thorough explanation! $\endgroup$ Apr 14, 2012 at 17:52
  • $\begingroup$ @RHall And I myself have made some useful discoveries during this fieldtrip :) $\endgroup$ Apr 14, 2012 at 18:04

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.