# “Auto-loading” a .m file

I have a .m (say e.g. file1.m) file with various definitions that gets sourced by another notebook using the command Needs["file1"]. If file1.m is in the ~/.Mathematica/Paclets/Repository, this works, but if it's in the same directory as the notebook, it doesn't. My question is the following: Can I create a directory in the ~/.Mathematica/Paclets/Repository and put the file1.m file there so that Needs["file1"] works (without giving the full path of the file1.m file)? Or I shouldn't because this directory is handled automatically by Mathematica? Is there another way around?

## 1 Answer

There seem to be some misunderstandings here.

I have a .m (say e.g. file1.m) file with various definitions that gets sourced by another notebook using the command Needs["file1`"].

Needs is only for loading packages, which must follow certain conventions. If you just have a file with definitions, use Get, not Needs.

For creating packages, see Creating Mathematica packages. This requires a lot more than just putting definitions in a file.

If file1.m is in the ~/.Mathematica/Paclets/Repository, this works ...

Can I create a directory in the ~/.Mathematica/Paclets/Repository and put the file1.m file there ...

Never, ever put anything into Paclets/Repository. That directory is managed by functions such as PacletInstall, and should not be modified manually.

If you created a package, and want to distribute it to other people, you may want to package it up into a paclet. This involves more than simply creating the package. See How to distribute Mathematica packages as paclets? for details.

If you just want to evaluate the contents of a file, use Get. Get will look in the current directory (Directory), as well as on the \$Path. It will not look in the notebook's directory, but you can change the current directory to that using SetDirectory[NotebookDirectory[]], or you can pass the full path to the file to Get.

• Hi, and thank you for your detailed response. I was actually contrasting CustomTicks that has only a .m file with MaTex, because the latter can be found in /Paclets/Repository whereas the former it's not. – hal Apr 8 '20 at 13:05