V 12.1 on windows.
I am loading pkg2
, which in its Private section loads pkg1
in order to call one of its function.
After using pkg2
, I found that pkg1
is now visible and can be called without loading it.
I thought the whole idea of loading a package from the private section of another package, is to keep it private and not to leak it to the top user level. Otherwise, why load it from private section in first place?
I am using the layout based on this answer load-a-mathematica-package-from-within-a-package
Here is MWE. First I have the two packages in same folder the main notebook for now to make it easier to test
Here is pkg2.m
. This is the one I want to use,
BeginPackage["pkg2`"];
Unprotect@@Names["pkg2`*"];
ClearAll@@Names["pkg2`*"];
test::usage
Begin["`Private`"];
Needs["pkg1`"];
test[]:=Module[{},
Print["Inside test[] in pkg2"];
pkg1`foo[]
]
End[]; (* `Private` *)
Protect@@Names["pkg2`*"];
EndPackage[]
I used Needs
in the above. Get
also works. It seems Needs
found pkg1.m
since it is in same folder as pkg2.m
automatically without having to give full path. Notice that the caller sets the current directory to where the packages are, and I think this is why it worked without the full path. Mathematica seems to search current folder first.
And here is pkg1.m
BeginPackage["pkg1`"];
Unprotect@@Names["pkg1`*"];
ClearAll@@Names["pkg1`*"];
foo::usage
Begin["`Private`"];
foo[]:=Module[{},
Print["in pkg1 foo OK"]
]
End[]; (* `Private` *)
Protect@@Names["pkg1`*"];
EndPackage[]
And here is main.nb
, which loads pkg2.m
. And once this package is loaded, then pkg1
becomes visible to the user as well, which is not what I want
SetDirectory[NotebookDirectory[]]
Get["pkg2.m"]
?? pkg1`*
Question is : What is the correct to have package A use package B in its private section without making B visible to the user?