Skip to main content

kernel - Autoload a package


I want to aoutoload a package at the beginning of a notebook. I set up my directory as written here


How to use the Autoload directory?


So, I have these files:



../Work/MyFile.nb


../Work/Autoload/MyPack/MyPack.m


../Work/Autoload/MyPack/Kernel/init.m.




MyPack.m :


ClearAll["Progetto`*"];  

BeginPackage[ "Progetto`"]

Prova::usage="Prova[]"

Begin["`Private`"];

Prova[]:=5+1;


End[]

EndPackage[]

init.m:


 Get["MyPack`MyPack`"]

In MyFile.nb, I would like use Prova[], but the output isn't 6 but is Prova[].


Where am I wrong? Am I missing something in init.m?




Answer




I want to aoutoload a package at the beginning of a notebook.



In this case, create an initialization cell in the notebook, and load that package in that cell. Select the cell, and tick Cell menu -> Cell Properties -> Initialization Cell.


"Autoloading" refers to loading something on kernel startup, and is completely independent of notebooks. This is not what you need.



So, I have these files:



../Work/MyFile.nb



../Work/Autoload/MyPack/MyPack.m




The Autoload directory I wrote about in the answer you are linking to is located in $UserBaseDirectory. There is only one* such directory, and it is used on kernel startup. It does not interact with notebooks in any way.


* To be completely accurate, there are a few, but they all live in fixed locations, such as $UserBaseDirectory, $BaseDirectory, $InstallationDirectory/SystemFiles, etc.


Comments