In your situation, the standard approach is usually different. This means, I use something like this only
BeginPackage["PQR`" ,{"PQR`PQRVisualization`","PQR`PQRUtilities`"}]
when I need the functionality of the Visualization and Utilities inside my package, not to re-export them.
What about creating an directory structure the following way
PQR
├── Kernel
│ └── init.m
├── PQRUtilities.m
└── PQRVisualization.m
And in the init.m
you Get
your sub-packages. Placing this in folder which Mathematica can find lets you <<PQR`
and everything should work as expected.
Update:
If you want to have an example where such approach is used, then look at $InstallationDirectory/AddOns/Packages/BarCharts
. You load <<BarCharts`
, but there is no package BarCharts.m
, it's only a collection of different sub-packages which are loaded through the init.m
BarCharts/
├── Charts.m
├── Charts3D.m
├── Kernel
│ └── init.m
├── PacletInfo.m
└── Usage.m
And the Kernel/init.m
contains
Get["BarCharts`Usage`"];
Get["BarCharts`Charts`"];
Get["BarCharts`Charts3D`"];
There was a follow-up question from Verbeia in the comments:
This works great, thanks! But one quick question - is there a reason why it works for Get and gives an error Needs::nocont: Context PQR` was not created when Needs was evaluated. >> when using Needs
Yes, the reason is that Needs["PQR`"]
expects that a context PQR`
is actually created which is not the case if it is really just a dummy directory/package.
This means,
- when you set up the structure like I described
- and there is no
.m
file which is named after the parent package directory PQR
- and every sub-package only creates its own context by calling
BeginPackage["PQR`Sub`"]
then you get this warning when you call Needs["PQR`"]
.
If you inspect now the BarCharts example in more detail, you see that they, although using different file names, always use BeginPackage["BarCharts`"]
and therefore introduce there different functions in the same namespace/context. The question is here, what you want to achieve with your package structure, because after loading your package all exported functions are visible without you noticing they are in different sub-packages/contexts.
What I usually do is the following: When my packages contain several sub-packages, then there is still one main-package which provides core functionality. To give an specific example: An image processing package IP
could provide basic functions under IP/IP.m
while addressing more special things in sub-packages like IP/Segmentation.m
. The simplest solution for you is therefore to make a package file PQR/PQR.m
which introduces the namespace/context PQR`
and the message will be gone.
Note that if some subpackages use functions that are defined in other subpackages, you will need to order the calls to Get
in init.m
carefully.
if I call the subsidiary packages directly with Needs equivalent, I get predictive code completion pop-ups including my custom functions.
............................ BUT (a) does the little drop down triangle appear that autocompletes WITH the function arguments?? and (b) if you click on the little HELP button on the RHS (which is meant to open the Help page for that function), does that work? Because neither (a) nor (b) are working for me for third party packages. I reported this to Wolfram, and got back a reply saying it was a bug re (b). I didn't report issue (a). $\endgroup$a`b`c
) don't auto-complete. I suggested to support that this would be nice to have. If you also do, maybe they'll implement it. $\endgroup$