4
$\begingroup$

In Mathematica 14.0.0 (MacOS Sonoma 14.4.1) I made a simple package consisting of two "sub packages". One of them is loaded as "private" from with the main package:

The package structure is in the subdirectory LP in my user directory (MacOS /Users/twan:

LP/
   Kernel/
       init.wl
   Main.wl
   Sub.wl

The contents of the files is as follows: For init.wl:

(* ::Package:: *)

Get["LP`Main`"]

For Main.wl:

(* ::Package:: *)

BeginPackage["LP`Main`"]

dummy::usage = "dummy[s] with s a string to be formatted by the format function in LP`Sub"

Begin["`Private`"]

Needs["LP`Sub`"]

dummy[s_] := subformat[s]

End[]

EndPackage[]

For Sub.wl:

(* ::Package:: *)

BeginPackage["LP`Sub`"]

subformat::usage = "subformat[s] format string s, say, to uppercase"

Begin["`Private`"]

subformat[s_]:= ToUpperCase[s]

End[]
EndPackage[]

When I do: FindFile["LP`"] the function returns with "/Users/twan/LP/Kernel/init.wl" as expected.

However, when I do: Needs["LP`"] it complains:

Context LP` was not created when Needs was evaluated

I've no clue what has gone wrong, because everything seems to work. Checking the $ContextPath gives: "LP`Main` , ..." as desired, moreover, dummy works as expected.

dummy["test"]
TEST

My questions are:

  1. What and why does Mathematica complains with the message?
  2. Except from Off[Needs::nocont], what do I do to prevent this?

This is a very simplified version of my big project and it behaves a bit different but with a similar annoyance: the same message occurs when Main.wl loads Sub.wl.

Thanks for reading this far anyway.

$\endgroup$

2 Answers 2

4
$\begingroup$

The message says "Context LP` was not created...", and looking at your code I don't see any line that would create the context "LP`", so that is your error. Specifically, I don't see any code that says

BeginPackage["LP`"]
....
EndPackage[]

You create two contexts, "LP`Main`" and "LP`Sub`", but not "LP`".

This is what I would do,

  1. Delete the init.wl file. Packages haven't needed these files since before I started using Mathematica.
  2. Move the Main.wl and Subl.wl files into the Kernel folder
  3. Rename Main.wl to LP.wl, and change the lines BeginPackage["LP`Main`"] to BeginPackage["LP`"]

Hope that helps.

$\endgroup$
1
  • $\begingroup$ B, thanks, that seems to do the trick. Do I understand correctly that I need to "build" a context using BeginPackage every time I want a "deeper" level, Similar to creating subdirectories in a file system)? $\endgroup$
    – nanitous
    Commented Jun 15 at 9:14
4
$\begingroup$

There are lots of magics behind the old-styled packages, e.g.

Many old-style applications use a special kernel feature for locating a file that corresponds to a context: If a context maps to a file location on $Path, but no appropriately-named .wl or .m file is found there, the kernel looks for a Kernel subdirectory with an init.m file in it, and reads that file. The init.m file typically just says something like Get["MyContext`MyContext`"]. The double context will correctly map to a MyContext.m file in the MyContext parent directory. This is really just a hack to allow the context MyContext` to map to a MyContext.m file in a MyContext directory on $Path.

in Paclets and Paclet Development

I personally suggest using this new paclet style, without those mysteries behaviors, instead of answering your question.

Examples are like PacletCICD-Examples-Sample and PacletCICD-Examples-AdvancedSample, the latter of which provides a convenient way of implementing subpackages.

$\endgroup$
5
  • $\begingroup$ thanks for the reference. I certainly will look into it. It is the automagical mechanisms which are confusing. Not only because any documentation on the interaction of that magic is difficult to find as it is spread through the documentation. $\endgroup$
    – nanitous
    Commented Jun 15 at 14:09
  • $\begingroup$ The WL reference to Packet dev you mentioned contains language like "...usual conventions for paclet layout..." Now, what are those "usual conventions"? (Rhetorical question) I find that hardly confidence inspiring documentation. $\endgroup$
    – nanitous
    Commented Jun 17 at 4:45
  • $\begingroup$ @nanitous some of the fields have default values when you do not specify them (although not documented). You could just skip those words and get some feelings on paclet, and then look at examples, like the ones written by wolfram teams in https://resources.wolframcloud.com/PacletRepository/all-publisher#w. There are also lots of official paclets hidden in the installation directory of MMA. $\endgroup$
    – Lacia
    Commented Jun 17 at 5:33
  • $\begingroup$ @nanitous reference.wolfram.com/language/tutorial/Paclets.html seems to be the up-to-date documentation of paclet. Sorry about that. $\endgroup$
    – Lacia
    Commented Jun 17 at 5:40
  • 1
    $\begingroup$ Your link to the documentation was quite okay. I meant that the documentation itself is, in my opinion, lacking details on several deeper and detailed aspects on creating packages and paclets. So, please, sorry is not needed ;-) $\endgroup$
    – nanitous
    Commented Jun 17 at 8:58

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.