I'm encountering a problem when trying to write and load my own packages. As a test case, I created a very simple package, TestPackage.m, which contains


TestPackage::"usage" = "This is a usage message.";

TestPackage := Module[{},
  Print["You called the TestPackage function"]


If I evaluate the cell containing this code, then when I evaluate TestPackage in a notebook I see You called the TestPackage function.

If I instead (i) restart the kernel, (ii) save a notebook (in the same directory as TestPackage.m) with the following code, and execute it, I see

$Path = Append[$Path, NotebookDirectory[]];
(*Context TestPackage was not created when Needs was evaluated*)

The problem doesn't seem to be the path, because that generated a different error before I fixed it. Why might I be getting this error message about context?

I don't know if it is related, but I've found that when I click the "Run All Code" button that appears in the upper right corner of open package file window, I have the same problem as when I try to use Needs[], unlike when I select the cell and evaluate it.

Responses to questions in comments


yields the NotebookDirectory[] path followed by TestPackage.m



(* ::Package:: *)

(* ::Input:: *)
(*TestPackage::"usage" = "This is a usage message.";*)
(*TestPackage := Module[{},*)
(*Print["You called the TestPackage function"]]*)
  • $\begingroup$ What do you get from FindFile["TestPackage`"]? $\endgroup$ – Jason B. Aug 24 '17 at 18:54
  • $\begingroup$ And assuming FindFile returns a file name, what does Import[FindFile["TestPackage`"], "String"] return? $\endgroup$ – Carl Woll Aug 24 '17 at 18:58
  • $\begingroup$ @JasonB. Results added to the post above. No problems with FindFile as far as I can tell. $\endgroup$ – FalafelPita Aug 24 '17 at 19:33
  • 7
    $\begingroup$ There's your problem. All of the code in the package is commented out. How did you create the package? Most likely you used the Front End to create the package, and all of your inputs were in "Input" cells, not "Code" cells. Only cells with Initialization->True get stored in the package without comments. Code cells have Initialization->True, Input cells do not. $\endgroup$ – Carl Woll Aug 24 '17 at 19:33
  • $\begingroup$ I wrote it up in a notebook, then went to save as and chose Wolfram Mathematica Package from the dropdown menu. Thanks for the tip about Input vs. Code cells. $\endgroup$ – FalafelPita Aug 24 '17 at 19:38

Browse other questions tagged or ask your own question.