Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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