5
$\begingroup$

I am building a package, using Mathematica 10.0.2.0. I notice in the displayed window for the package there are buttons labeled Update and Run Package; on the File menu, there is an item for Save; there is also an item on the same menu called Install, with an option for packages.

My question is when should I use each of these possibilities? In particularly, if I have a notebook that Gets the package, and I make a change to the package, which of these possibilities do I need to invoke in order for the notebook to have access to the changes in the package? Do I have to do anything different the first time I edit the package?

$\endgroup$
6
$\begingroup$

A Mathematica package is just a file with the .m or .wl extension that contains Mathematica expressions. Get will simply evaluate these expressions.


The "Run Package" button will simply evaluate everything in the package file. It's equivalent to Get["/path/to/mypakcage.m"].

The "Update" button rebuilds the Function and Section menus just left of this button.

"File -> Save" will save the file you are editing. It's the same as "Save" in all text editors.

"File -> Install..." will can be used to install packages. Usually this simply means copying the package file to FileNameJoin[{$UserBaseDirectory, "Applications"}].


Regarding the question:

In particularly, if I have a notebook that Gets the package, and I make a change to the package, which of these possibilities do I need to invoke in order for the notebook to have access to the changes in the package?

It is important to understand that all Get["file"] does is evaluate all the expressions in file. There's nothing more to it. The fact that these expressions are saved in a file with the .m extension makes no difference to this. You could evaluate the same expressions in a notebook instead of using Get to read them from a file, and the effect would be the same.

This gives you the answer to your question. Re-loading the package either with Get or the Run package button may or may not correctly update the functions in the package, depending on what was changed.

If you had f[x_] := x and change it to f[x_] := x^2, then it will override the previous definition.

If you had f[x_] := x and change it to f[x_, y_] := x*y, this will just add an extra definition for f without removing the previous one. This is clear once we understand that all we're doing is evaluating one definition after the other.

If you follow the standard package structure, localizing everything to a context, then you might use Clear["MyPackage`*"] to remove the package definitions (all residing in MyPackage`) before reloading the package.

$\endgroup$
  • 1
    $\begingroup$ @Szabolics. Very nice answer, however, you state: "You could evaluate the same expressions in a notebook instead of using Get to read them from a file, and the effect would be the same." There are some subtle gotchas where this is not true. Try Print["hi"] in a notebook, and you get "hi" displayed in the notebook. Try Get containing code with Print["hi"], and Mathematica prints to a Messages notebook, not the notebook that called the package. Subtle, but sometimes important. $\endgroup$ – Todd Allen Jul 7 '15 at 15:20
  • $\begingroup$ @ToddAllen Good point that there are small differences. I wanted to mention this in a footnote (not to distract from the main point), but forgot. Things like $InputFileName are only active during Get and outputs are not printed. But this specific difference you mention with Print I do not see ... the output always goes into the notebook. I wonder what setting controls that. $\endgroup$ – Szabolcs Jul 7 '15 at 16:55
  • $\begingroup$ The scenario I mentioned above happened to me recently on a project I was working on. Specifically, I was using a palette notebook (installed) to create a document (CreateDocument), and then inside that I used Get to call a script containing Print. The only way I could get my Print output to display inside the created document, was to replace Print with TextCell statements. I'm not sure if this behavior is linked to some special attributes of palettes (or created documents) or perhaps $Output. (Also, my apologies for misspelling your name in my first comment.) $\endgroup$ – Todd Allen Jul 8 '15 at 16:38
  • $\begingroup$ @ToddAllen That's due to the palette, not the Get. Print will send output to the message window when invoked from a palette. Don't worry about the name, I know it's unusual :) $\endgroup$ – Szabolcs Jul 8 '15 at 19:11

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.