2

I have a package file MyPackage.wl it has the following commands:

BeginPackage["Me`MyPackage`",{"ExternalContext1`","ExternalContext2`"}]
...
Needs["Me`MyPackage`Internal`"];
...
EndPackage[];

Now within Internal.wl it has the following commands:

BeginPackage["Me`MyPackage`Internal`",{"Me`MyPackage`"}];
...
EndPackage[];

Now I can access the "Me`MyPackage`Internal`", "ExternalContext1`" and "ExternalContext2`" symbols inside MyPackage.wl but within Internal.wl I can only access "Me`MyPackage`" symbols but not "ExternalContext1`" and "ExternalContext2`".

So why are the contexts "ExternalContext1`" and "ExternalContext2`" not being added to the $ContextPath inside Internal.wl. I can always start Internal.wl by:

BeginPackage["Me`MyPackage`Internal`",{"ExternalContext1`","ExternalContext2`","Me`MyPackage`"}];

and it would work but as I add more and more ExternalContexts and there are more and more subcontexts, I don't want to keep updating this list inside every single subpackage. What is the better way of doing this?

user13892
  • 9,375
  • 1
  • 13
  • 41

1 Answers1

2

BeginPackage["pak1`",{"pak2"}] loads pak1 and pak2, but no package that pak2 needs. You can see this by e.g.:

BeginPackage["external`"]
$ContextPath
EndPackage[]

BeginPackage["context1", {"external"}] $ContextPath EndPackage[]

And then:

BeginPackage["internal`", {"context1`"}]
$ContextPath
EndPackage[]

internal`

{"internal", "context1", "System`"}

"external"`is not loaded.

To load also packages that a needed package needs, you may define a variable "cPath" that contains the loaded packages like:

BeginPackage["context1`", {"external`"}];
cPath = $ContextPath;
EndPackage[];

BeginPackage["internal", {"context1"}] Needs /@ context1`cPath; $ContextPath EndPackage[]

This returns:

context1`
{"external`", "internal`", "context1`", "System`"}

Now "external`" is also loaded.

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57
  • Thank you, I am keeping a variable to keep track of contexts as you suggested. But I am not accepting the answer in case their is a more builtin paclet way of doing this. – user13892 Feb 10 '24 at 19:52