Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a structure of folders.

  1. Main Directory( has init.m, and name.m files)
  2. Subfolders under Main directory
    1. library ( has the packages, pacakge1.m, package2.m)
    2. Data (some xcel sheets and other data files) The init.m has the following commands Get[FileNameJoin[{NotebookDirectory[],"library", "Package1.m"}]]; Get[FileNameJoin[{NotebookDirectory[],"library", "Package2.m"}]];

Package1 has function1 and function2 definitions. Package2 has function3 and function4 definitions.

The Names.m file has the following the commands

DeclarePackage["Package1`",{"function1", "function2"}];
DeclarePackage["Package2`",{"function3", "function4"}]

After evaluating the name.m file, I was expecting that calling function1 should execute the Package1`function1. However it is not happening. This is what I understand of Mathematica so far. I don't want to touch the init.m file in the Autoload folder of the userbase directory as I don't want to modify the contents. I just want my init.m in the local folder to run.

Workarounds anyone?

share|improve this question
In the second argument of BeginPackage you can specify additional contexts/packages that are needed. Is that what you want? – Mr.Wizard Feb 17 '13 at 11:14
Play around with the function FindFile until you get the relationship between context name and folder structure. Both \\package\\init.m, or \\package.m are found with "package`", as long as the base directory is in the path, no need to specify the full file name. Subcontexts are interpreted as subfolders. – Rojo Feb 17 '13 at 16:26
Somewhat related. – Leonid Shifrin Feb 17 '13 at 17:35

From the documentation:

NotebookDirectory[] gives the directory of the current evaluation notebook.

Your init.m is not the evaluation notebook and therefore this will not work. A simple test-case can be constructed when you create a test.m with a single line of code


Then you open a new and unsaved notebook and Get this package. It will lead to an error

NotebookDirectory::nosv: The notebook NotebookObject[Untitled-1] is not saved. >>

Why don't you look at the directory structure of packages in the e.g. AddOns folder and follow the usual conventions?

share|improve this answer

Your Answer


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.