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 have some packages inside a folder (named Packs) that is in the same file level of some executed package.

executedPack.m
Packs/subPack01.m
Packs/subPack02.m
Packs/subPack03.m

Today I use this structure do load the sub packages inside the executedPack.m:

(*Execution without Front-End*) 
Needs["MyPack`",FileNameJoin[{DirectoryName@$InputFileName,"Packs","subPack01.m"}]]

(*Execution with Front-End*)    
Needs["MyPack`",FileNameJoin[{NotebookDirectory[],"Packs","subPack01.m"}]]

The first I use when running the package without front-end, and the last when I evaluate in the front-end.

There is some simpler way to do that, more symmetric? I read some kernel folder structure, but I didn't find more details on that.

Update

Now what I want, but better. This works both in Fron-End and no Front-End execution.

Quiet[Check[SetDirectory@DirectoryName@$InputFileName,SetDirectory@NotebookDirectory[]],SetDirectory::fstr];
Needs["Packs`subPack01`"];
share|improve this question
    
For a start you could look at: Custom Package Development –  David Park Aug 8 '13 at 14:31
    
Interesting David, tks. –  Murta Aug 8 '13 at 15:55
add comment

2 Answers

You you could use the following code in place of your example above.

Needs["MyPack`", FileNameJoin[{
   If[$InputFileName == "", NotebookDirectory[], $InputFileName]
   , "Packs", "subPack01.m"}]]

Or you could use Module like the following.

Module[{$InputFileName = 
       If[$InputFileName == "", NotebookDirectory[], $InputFileName]},
     Print[$InputFileName];
 ]
share|improve this answer
add comment

After some google and forum search, I get this:

Quiet@SetDirectory@NotebookDirectory[];
Needs["Packs`subPack01`"];

To avoid alert msg, subPack01 is declared inside it as:

BeginPackage["Packs`subPack01`"]

this is a nice article, tks @DavidPark for the reference

share|improve this answer
add comment

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.