8
$\begingroup$

This question already has an answer here:

I'm using IntelliJ Idea with a Mathematica plugin to develop Mathematica application. I keep IntelliJ and Mathematica opened at the same time. I would like to change something in the IDE, evaluate notebook in the Mathematica and see updated result, but Mathematica fails to see changes in my code.

My application consists of several subpackages, and Kernel/init.m looks like

Get["MyPackage`A`"]
Get["MyPackage`B`"]
...

Notebook is something like

Quit[]
Get["MyPackage`"]
SomeFunctionFromMyPackage[]

And I've also tried

Needs["Utilities`CleanSlate`"] (* inside initialization cell *)
CleanSlate[]
Get["MyPackage`"]
SomeFunctionFromMyPackage[]

Let's say I changed SomeFunctionFromMyPackage, saved .m file` hit "Evaluate notebook" and hoping to see result changing, but this doesn't happen. Result is not getting updated. I've tried to introduce syntax error in function, Mathematica doesn't react at all.

The only way to force Mathematica to actually reload my package is to restart it.

It's driving me crazy, what am I doing wrong?

Upd

Such strange problems were caused by the fact that subpackages inside application depend on each other with Needs instead of Get. When I changed it to Get the problems went away.

$\endgroup$

marked as duplicate by Leonid Shifrin, Feyre, mikado, Mr.Wizard Oct 16 '16 at 0:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ So you say you actually quit the kernel and loaded the package again, and it didn't see the changes? But when you restarted the front end then it did? That sounds rather unlikely, so please check carefully again. $\endgroup$ – Szabolcs Oct 15 '16 at 17:44
  • $\begingroup$ @Szabolcs, ok, I checked again. The problem is in "Evaluate notebook". When I manually evaluate cells from top to the bottom, it works as expected. Why? $\endgroup$ – lambdas Oct 15 '16 at 17:50
  • $\begingroup$ Probably because Quit[] cancels all further evaluations by default. $\endgroup$ – Szabolcs Oct 15 '16 at 17:51
  • 1
    $\begingroup$ @Szabolcs, ouch, I see.. Then my question stays the same, what is convenient and reliable way to reevaluate notebook after package changed? $\endgroup$ – lambdas Oct 15 '16 at 17:53
  • $\begingroup$ I once wrote a package to cleanly reload packages. A brief description is in this post. $\endgroup$ – Leonid Shifrin Oct 15 '16 at 18:39
11
$\begingroup$

There isn't a fully general way to reload packages because packages can do just about anything in principle, not just create definitions.

I recommend that you write your package in a way that simply using Get again will properly re-set its state. Assuming that all your package does is issue definitions, you could do something like this:

BeginPackage["MyPack`"];

Unprotect["MyPack`*"];
ClearAll["MyPack`*"];
ClearAll["MyPack`Private`*"];

Foo::usage = "";

Begin["`Private`"];

helper[x_] := x^2
Foo[x_] := 2*helper[x]

End[];
Protect["MyPack`*"];
EndPackage[];

But in general there's a lot more a package can do than issue definitions. For example, it can cache results (MaTeX), it can load or save some persistent configuration (MaTeX, MATLink), it can create temporary files and directories to work with, or some other kind of state that persists throughout the session, such as connecting to other programs (MATLink), it may load LibraryLink functions that must be unloaded before they can be safely reloaded (IGraph/M), etc. My point is that most packages that I contributed to had something else than definitions in them that made reloading problematic. Thus not all of them will fully reset their state when using Get a second time, and sometimes I just need to manually quit the kernel before loading them again. But I do try to build in some robustness against reloading to make development easier (and accidental double loading by users should definitely not break things).

$\endgroup$
  • $\begingroup$ It's a nice solution, thank you! There are no side effects in my packages, so I'll go with it. It turned out that problem is caused by subpackages depending on each other with Needs instead of Get. $\endgroup$ – lambdas Oct 15 '16 at 20:27
  • $\begingroup$ This does not seem to reload the usage definitions. Is there a workaround? $\endgroup$ – Michael McCain Jan 3 '17 at 11:34
  • $\begingroup$ @MichaelMcCain Can you show an example? I don't see why it wouldn't enable changing usage messages. $\endgroup$ – Szabolcs Jan 3 '17 at 11:36
  • $\begingroup$ @Szabolcs - We can use the exact code in the this thread to illustrate. I first executed the package notebook. Then I usedGet["MyPack"]` in a new notebook to install it. Then I verified it was installed. Then I tried changing the line in the package where is says Foo::usage = ""; to Foo:usage = "testing". I then re-executed Get["MyPack"]` in the other notebook. No change. :/ $\endgroup$ – Michael McCain Jan 3 '17 at 11:54
  • $\begingroup$ sorry for the quotation single and double quotations problems in my question... not sure how to fix that. $\endgroup$ – Michael McCain Jan 3 '17 at 11:55
9
$\begingroup$

I use these two regularly when developing packages. Not entirely bullet-proof, but enormously helps my workflow. For convenience, you should simply use Reload instead of Needs. Whatever is changed in the package file will be reloaded.

Attributes[Unload] = {Listable};
Unload::nonun = "The System`.` and Global`.` contexts cannot be removed.";
Unload["System`" | "Global`"] := Message[Unload::nonun];
Unload[form_String, fun_: Remove] := 
  Module[{found = Cases[$Packages, _?(StringMatchQ[#, form<>"*"] &)], all},
   all = # <> "*" & /@ found;
   Unprotect /@ all;
   Quiet[fun /@ all];
   Unprotect@$Packages;
   $Packages = DeleteCases[$Packages, Alternatives @@ found];
   Protect@$Packages;
   $ContextPath = DeleteCases[$ContextPath, Alternatives @@ found];
   ];

Attributes[Reload] = {Listable};
Reload[pkg_String] := (
   If[MemberQ[Union[$ContextPath, $Packages], _?(StringMatchQ[#, pkg<>"*"] &)],
     Unload[pkg, ClearAll]]; 
   Get@pkg
   );

Note, that Unload removes symbols from other packages as well, e.g. Unload["Language`"] also removes other system-Language` related functions, not just user-Language` ones.

$\endgroup$
  • $\begingroup$ Thank you for answer, but it doesn't work for me. I defined Reload and Unload in the same notebook, I guess it's ok. And I'm calling Reload["MyPackage`"] instead of Get["MyPackage`"]. It doesn't give any errors, but symptoms are the same. Does Reload work correctly for subpackages? $\endgroup$ – lambdas Oct 15 '16 at 19:53
  • $\begingroup$ Sorry, your functions actually work great. My packages depend on each other with Needs instead of Get and it caused strange problems. $\endgroup$ – lambdas Oct 15 '16 at 20:23
  • $\begingroup$ @lambdas It works now... Had to add proper string matching to handle subcontexts, thanks for pointing it out! $\endgroup$ – István Zachar Oct 15 '16 at 20:39

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