Can someone offer some advice on how to get started with mathematica packages?
I can save the following in a file named "Foo.m". When I run the input cell in that file, I can see the effects reflected in $ContextPath variable.
BeginPackage["Foo`"]
bar::usage = "barfunction";
Begin["`private`"]
bar[w_] := w;
End[];
EndPackage[];
However, in my notebook I have the following:
#include<foo>
SetDirectory[ToFileName[{$HomeDirectory, "My Documents", "mathematica"}]];
Needs["Foo`"]
$ContextPath
But the needs call is failing for some reason, and the $ContextPath remains unchanged.
Edit
I believe that I've got a partial solution working now, the cell in my file wasn't marked as an initialization cell - and whilst I can now <<Foo'
, Needs["Foo
"]` still isn't working correctly.
Check to make sure the saved file, "Foo.m", is on your $Path
, which tells Mathematica which directories to look in when trying to load packages, much like the PATH
environment variable in Unix or Windows.
EDIT: $ContextPath
won't be changed unless there's an actual BeginPackage
statement (or you manipulate it directly using Set
or Block
or something).
EDIT the second: One thing to check is what
FileNames["Foo.m", $Path]
returns. What you're describing does sound a little strange, though.