4
$\begingroup$

As an introduction to packages in Mathematica I'm following this short guide by Leonid Shifrin, and tried using the sample package provided in that answer. However, both Needs and Get seem to fail loading the package.

Issuing Needs I get the error

Needs::nocont: "Context SimpleArithmetic` was not created when Needs was evaluated.

Using Get I don't get any error, but the package is still not loaded.

Here is what I did (I'm using M10.1 with Windows 7, if that is not obvious from the screenshots :) ):

  1. Copy-pasted the code in the answer linked above in a new notebook, saved the notebook with .m extension inside the $UserBaseDirectory\Applications folder:

enter image description here

  1. Opened a fresh Mathematica session and used Needs to load the package:

enter image description here

As you can see something is going wrong. What am I missing?

$\endgroup$
  • 1
    $\begingroup$ I think the pointer that Jack gives in his answer is correct, but you can achieve the same effect without using a notebook. Just select all cells in the package file opened in Mathematica as shown in your screenshot and convert everything to code cells (use the menu "Format" -> "Style" -> "Code or just Alt-8). Your code seems to be all in one input cell which is only contained as comment in the package file. $\endgroup$ – Albert Retey Aug 22 '15 at 20:04
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/q/9921/5 $\endgroup$ – rm -rf Aug 22 '15 at 20:57
5
$\begingroup$

Check the SimpleArithmetic.m file and make sure that the code is not commented out.

A technique that works is to name the notebook SimpleArithmetic.nb.

Take the lines that you have copied and make them individual cells (one for each command) rather than a single cell.

Set all of their properties to Initialization Cell. You can select them all (ctrl-A) and set them to Initialization Cell in one fell swoop.

Enable AutoGeneratePackage via:

Format -> Options Inspector -> Notbook Options -> File Options -> AutoGeneratePackage -> Automatic.

Make sure that the selection is the current notebook in the above step.

Save the notebook.

A separate file called SimpleArithmetic.m should get created.

Check it with a text editor.

This should work with your Needs or Get command.

$\endgroup$
  • $\begingroup$ Thank you very much, this did the trick! I didn't really follow your steps though, as for example the AutoGeneratePackage option was greyed out and I couldn't change it. What I did was to verify with a text editor if the code in the generated .m file was commented out as you suggested (and indeed it was). Then I copy-pasted again the code in a new notebook, but before saving it as .m file I set the cell (I used a single one) containing the code as an "Initialization Cell". This was enough for the .m file to be generated not commented out, and Needs now works as expected. $\endgroup$ – glS Aug 22 '15 at 19:27
  • $\begingroup$ Now however, I'm curious as to where in the documentation is all of this procedure explained. I couldn't find it. $\endgroup$ – glS Aug 22 '15 at 19:28
  • $\begingroup$ @glance - Search for AutoGeneratedPackage in the documentation. You will find it there. If it was greyed out I think you need to change the Show option values at the top from Selection to Selected Notebook. $\endgroup$ – Jack LaVigne Aug 22 '15 at 23:03

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.