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
BeginPackage["TestPackage`"];
TestPackage::"usage" = "This is a usage message.";
Begin["`Private`"];
ClearAll[TestPackage];
TestPackage := Module[{},
Print["You called the TestPackage function"]
]
End[];
EndPackage[];
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[]];
Needs["TestPackage`"]
(*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
FindFile["TestPackage`"]
yields the NotebookDirectory[] path followed by TestPackage.m
Import[FindFile["TestPackage`"],"String"]
Produces
(* ::Package:: *)
(* ::Input:: *)
(*BeginPackage["TestPackage`"];*)
(**)
(*TestPackage::"usage" = "This is a usage message.";*)
(**)
(*Begin["`Private`"];*)
(*ClearAll[TestPackage];*)
(*TestPackage := Module[{},*)
(*Print["You called the TestPackage function"]]*)
(*End[];*)
(**)
(*EndPackage[];*)
FindFile["TestPackage`"]? – Jason B. Aug 24 '17 at 18:54FindFilereturns a file name, what doesImport[FindFile["TestPackage`"], "String"]return? – Carl Woll Aug 24 '17 at 18:58FindFileas far as I can tell. – FalafelPita Aug 24 '17 at 19:33