I have more or less the following setting. In ~/path/to/my/packages
I have two packages package1.m
and package2.m
. Each package's outline is, for example, the following:
BeginPackage["package1`"]
Unprotect@@Names["package1`*"];
ClearAll@@Names["package1`*"];
Begin["`Private`"]
vecNorm[vec_?VectorQ]:=Module[{},Return[Sqrt[vec.vec]]];
End[]
Protect@@Names["package1`*"];
EndPackage[]
Now, my problem is that I want to use vecNorm
defined in package1.m
in package2.m
. How can I load (safely) package1
from within package2
?
At the moment, I load manually both packages as follows:
SetDirectory[StringJoin[NotebookDirectory[], "packages"]];
Needs["package1`"]
Needs["package2`"]
from a notebook saved in ~/path/to/my
. I want to load manually only package2
which in turn will load automatically and safely package1
. In general I want a solution which changes as little as possible paths etc. of mathematica. What should be the best practice to accomplish this?
PS: By safely I mean that in the future, when I'll define package3
which will be using vecNorm
as well and will be loading package1
as well no conflicts will happen.