# Problem with Creating Mathematica Package

Currently I am working on creating the package on Mathematica version 9 on Windows 7. Here is my code as follows:

BeginPackage["mypackage"]

Begin["Private"]

getColumn[data_,branch_List]:=
Module[{pos},
pos = Position[data,#][[1,2]]&/@branch;
data[[All,pos]]
];

RemoveMissing[data_]:=DeleteCases[data,{date_,val_}/;!NumberQ[val]];

End[]

EndPackage[]


As you can see from the above that it contains two defined functions. I saved it as mypackage.m on my working directory.

To make sure my working directory is the one that contains mypackage.m, I put SetDirectory[NotebookDirectory[]] in my notebook. Then, I call the package by using

Needs["mypackage"]


Evaluating, but it seems not to load, because when I call the function getColumn, the letters should turn black if it is loaded (now it's still blue). Also, I tried other ways

Needs["mypackage","mypackage.m"]


and

<<mypackage


Both are still not working.

I have two questions:

1. When we save .m file, is it necessary to have the same name as we defined in the BeginPackage[""]

2. Is there anything missing so my package does not work ? any suggestions ?

Notice how the active context changes as the package gets loaded line by line:

BeginPackage["mypackage"]

(* the active context is mypackage *)
(* whatever gets mentioned for the first time within this section
becomes part of the mypackage context *)
(* any symbol meant to be public, i.e. usable when the package is loaded,
should be mentioned here *)

Begin["Private"]

(* the active context is mypackagePrivate *)
(* whatever gets mentioned here first becomes part of mypackagePrivate *)

End[]

EndPackage[]


You did not mention these symbols in the public section of the package so they won't be accessible without explicitly writing their context when you load the package.

The key here is to mention every symbol that needs to be "exported" in the correct section so it becomes part of the mypackage context. This context gets added to the context path when you load the package, so any symbol it contains can be references by its short name (not explicitly prepending the context) when you use the package.

You had few issues. Try this. It worked now on my system. You needed Usage and better to also add the Unprotect and ClearAll

BeginPackage["mypackage"]

Unprotect @@ Names["mypackage*"];
ClearAll @@ Names["mypackage*"];

getColumn::usage = "getColumn[data,branch]"
RemoveMissing::usage = "RemoveMissing[data]"

Begin["Private"]

getColumn[data_,branch_List]:=
Module[{pos},
pos = Position[data,#][[1,2]]&/@branch;
data[[All,pos]]
];

RemoveMissing[data_]:=DeleteCases[data,{date_,val_}/;!NumberQ[val]];

End[]
Protect @@ Names["mypackage*"];
EndPackage[]


Now save this to mypackage.m, then

SetDirectory[NotebookDirectory[]]
Needs["mypackage"]
?mypackage*


getColumn[{{1, 2}, {3, 4}}, {1, 2, 3}]


• Why the Unprotect and ClearAll? (Usage symbols, or more exactly, creating the symbols in mypackage  instead of Private was the problem). – Ajasja Feb 5 '14 at 3:34
• Further to Ajasja's comment. Any symbol you define only within Private will stay invisible to the outside world (usually the Global that context you're in by default when you start mathematica up - if you define x in an empty notebook its really Globalx). Anything you want to be publicly visible you have to mention in someway before Private starts, e.g. yoursymbol::usage="some instructions" or even just {yoursymbol, yourothersymbol, etc...};. That way your symbol is created as YourPackageyoursymbol which is visible (from Global) and not as YourPackagePrivate`yoursymbol which is not. – Ymareth Feb 5 '14 at 8:49