2
$\begingroup$

My notebook can't load my package and I don't know why.

I called "testons.m" the file containing my package, I wrote inside it the following exemple code :

BeginPackage[ "Package`"]



MainFunction::usage = 
    "MainFunction[ x] computes a simple function."

  Begin[ "Private`"]

  MainFunction[ x_] :=
    Module[ {y},
      y = x^2;
      y + 1
    ]

  End[]

  EndPackage[]

I put it in the folder :C:\Users\Me\Dropbox\internship-2017\Mathematica\initialisation

I wrote in my working notebook :

AppendTo[$Path,
"C:\\Users\\Me\\Dropbox\\internship-2017\\Mathematica\\initialisation"]

And when I write later in the same notebook :

<< Package`

Mathematica answers me : "Get::noopen: Cannot open Package`."

I followed the exact instruction written here : https://reference.wolfram.com/workbench/index.jsp?topic=/com.wolfram.eclipse.help/html/tasks/applications/packages.html

Where is my mistake ?

Thanks

$\endgroup$
  • 1
    $\begingroup$ Try using another name for your package, because Package` context already reserved by Mathematica itself. $\endgroup$ – Shadowray May 24 '17 at 16:52
  • $\begingroup$ I did it but it does'nt solve the problem with another name. Few questions in the same time : Must the name of the package file be the same as the name of the package inside ? If I modify something inside the package file do I have to do a "refresh" in my notebook ? And can the folder names contains space or accents ? $\endgroup$ – StarBucK May 24 '17 at 16:59
  • 4
    $\begingroup$ Also see the third answer from the end here: community.wolfram.com/groups/-/m/t/879149 about "Private`" vs "`Private`" $\endgroup$ – Kuba May 24 '17 at 18:09
  • 1
    $\begingroup$ Did you create the file as a package from the beginning? It seems that a package is more than a notebook with .wl or .m extension - it appears that you must always open the notebook's File menu, select New, and choose Package. $\endgroup$ – Vito Vanin May 25 '17 at 0:31
  • 1
    $\begingroup$ @StarBucK I think you should call your package by its file name, not context name, so in your case <<"Testons"` . Package context can be different from its file name, but I think this is not good practice. $\endgroup$ – Pinti May 25 '17 at 7:25
2
$\begingroup$

Thanks to the comments above I have solved the problem. I think it would be nice to summarise how to avoid it.

First :

We have to add the folder where the package is in the $Path variable. This folder can contain spaces in its name without problem.

We have to save a package as a package file in mathematica (.wl in the menu). The package filename cannot contain "_" (maybe other special letters wont work as well).

To ensure that the functions of the package are loaded, its cells has to be put as initialization cells : menu "cell->cell properties->initialization cell".

Next :

The package is called by its filename not by the string between BeginPackage["thestring`"] in the packagefile. Do not forget the "`", even if we are talking about the filename. For example if the package filename is "test.wl", we have to call it by writing : << test`

The package filename and the package name (the thing we write inside the bracket of BeginPackage) can be different.

After each modifications of package file, we have to load again the package to take in account thoose modifications.

$\endgroup$
  • $\begingroup$ Let's call the package mypack. Especially if you're going to include separate documentation files (always a good idea!), a good practice is to put the mpack.m (or mypack.wl) package file in a subfolder, also named mypack, of your UserBaseDirectory. And then inside that create a Kernel subfolder and in that place an init.m file whose sole contents, as an initialization cell, is: Get["mypack`mypack`"]. $\endgroup$ – murray Jan 7 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.