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?

  • $\begingroup$ In the second argument of BeginPackage you can specify additional contexts/packages that are needed. Is that what you want? $\endgroup$
    – Mr.Wizard
    Feb 17, 2013 at 11:14
  • 1
    $\begingroup$ 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. $\endgroup$
    – Rojo
    Feb 17, 2013 at 16:26
  • $\begingroup$ Somewhat related. $\endgroup$ Feb 17, 2013 at 17:35
  • $\begingroup$ You can use $InputFileName instead of NotebookDirectory[] to refer to the filename of the currently evaluating .m file. $\endgroup$ Jan 25, 2021 at 20:18

1 Answer 1


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?


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.