# ContextPath and autocompletion with subpackages

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["mainsub3"]
Get["mainsub2"]
Get["mainsub1"]
Get["mainmain"]


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["mainsub1"]


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, "mainsub1"]


Unfortunately, this is not very nice and yields duplicates in the $ContextPath. Is there a better, automatic way to achieve this? ## 2 Answers 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["mainmain"]
Get["mainsub1"]
Get["mainsub2"]
Get["mainsub3"]


you have $ContextPath = {mainsub3, mainsub2, mainsub1, mainmain, ...}, 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.

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).