3
$\begingroup$

I'm developing a larger package which includes several subpackages. My problem is, that I can't introduce the symbols in the subpackages to the autocompletion by loading the main package, but by calling a subpackage.

Let's explain this with an example: I have a main package, called main which loads the subpackages sub1, sub2 and sub3. The directory should look like

main
├── Kernel
│   └── init.m
├── main.m
├── sub1.m
├── sub2.m
└── sub3.m

The package main loads all three subpackages in its BeginPackage statement, while the package sub1 loads sub2 and sub3. The respective init.m looks like:

Get["main`sub3`"]
Get["main`sub2`"]
Get["main`sub1`"]
Get["main`main`"]

The big problem I have is, that by calling

Needs["main`"]

I can see all symbols in main, but not those in the subpackages (though, they are usable).

By calling

Needs["main`sub1`"]

I can see all symbols in the subpackages without any problems but can't load main anymore, because then some definitions are redone, which leads to error messages about protected symbols. The most interesting thing is, that the $ContextPath includes the subpackages in both cases.

Is there a nice possibility to get the symbols of the subpackages into the autocompletion?


Update: By prepending the path of a subpackage to the $ContextPath, I can load the corresponding symbols of that package into the autocompletion.

$ContextPath = Prepend[$ContextPath, "main`sub1`"]

Unfortunately, this is not very nice and yields duplicates in the $ContextPath.

Is there a better, automatic way to achieve this?

$\endgroup$

2 Answers 2

1
$\begingroup$

Just to give additional information, what I found, at least in Mathematica version 9, is that one must load the subpackages after the main package in order to have autocompletion for the subpackages symbols. When we do this the subpackages appear before the main package in the $ContextPath, so when you have a init.m like this:

Get["main`main`"]
Get["main`sub1`"]
Get["main`sub2`"]
Get["main`sub3`"]

you have $ContextPath = {main`sub3`, main`sub2`, main`sub1`, main`main`, ...}, and voilà, we have autocompletion.

I don't understand why Mathematica "shadows" the subpackage symbols when they appear after the main package in the $ContextPath, but this is the solution I found.

$\endgroup$
1
$\begingroup$

I think you have deal with Mathematica application.

I know 2 way for solv this problem

1) Add path to your application in variable $Path. And then load with Get

AppendTo[$Path,"C:\\path\\to\\your\\application"];
<<main`

This code try to find init.m in your application and evaluate it.

2) Save your application in directory $UserBaseDirectory <> "\\Autoload" and it will load with mathematica start (withe the way described in previous way).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.