2
$\begingroup$

I have a package at /Users/me/Desktop/Mathematica/MyPackages/SamplePackage.m. It looks like this:

BeginPackage["SamplePackage`"]

bruh::usage = "bruh[ x] computes a simple function."
bruhpriv::usage = "bruhpriv[x] gives 5."

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

Begin["`Private`"]
bruhpriv[x_] := 5
End[]
EndPackage[]

Obviously I have no use for this, I am just trying to make a freaking package load. Anyways, if I open the file SamplePackage.m in Mathematica and run it then open a new notebook, I can access both bruh and bruhpriv by their short and long names. Further, SamplePackage' is in the list of

$Packages

and SamplePackage' is also in the

$ContextPath

bruh[4] gives 17 like it should, etc. However, if I do anything but run the file SamplePackage.m it won't work. I use AppendTo[$Path,the folder I want] and if I do <<SamplePackage.m it runs with no error message. If I do Get["SamplePackage.m"] it runs with no error message. I know it is finding the file because if I remove the AppendTo line I get the file no found error, as expected.

So after I do <<SamplePackage.m it does not matter what I do I cannot use bruh[x] or bruhpriv[x]. Typing them in just echoes back the input. Further, SamplePackage' is not in

$Packages

or

$Context Path

The only way to get the functions is to open the file and run the lines. What am I doing wrong?

EDIT: IMAGE IN RESPONSE TO MURRAY enter image description here

$\endgroup$
  • 1
    $\begingroup$ Your description is a bit vague. Can you give an accurate and complete, step-by-step description of what you did? "This is what I did, step-by-step, in full detail. This is what I expected to happen. This is what happened." Without this information, people will simply tell you that it works for them (as you can already see). I expect there is some detail that you may assume to be unimportant (and so did not mention) but is actually causing the problem. $\endgroup$ – Szabolcs Jun 19 at 18:28
  • 1
    $\begingroup$ Be sure to include complete, runnable commands, with actual full paths (not edited examples). Also include the output of FindFile["SamplePackage`"]. $\endgroup$ – Szabolcs Jun 19 at 18:32
  • $\begingroup$ so I finally got it to work, I realized when I ran FindFile["SamplePackage.m"] it failed. I'm not sure what I did wrong before but I added the file to the $Path and then ran <<SamplePackage.m and it worked. I could have sworn I properly added the file to the path before. Whatever, it works now! $\endgroup$ – Brandon Jun 20 at 3:52
  • 1
    $\begingroup$ I think part of my error was that Mathematica did not search subdirectories in the path list which I assumed it would. perhaps before I had it in a subdirectory. this time I added the direct parent directory to the path and it worked $\endgroup$ – Brandon Jun 20 at 3:54
  • 1
    $\begingroup$ No, it does not search subdirectories. This could have been spotted immediately if you show a complete example, with actual paths, as I asked. $\endgroup$ – Szabolcs Jun 20 at 9:20
8
$\begingroup$

One way:

   AppendTo[$Path, "~/Desktop/Mathematica/MyPackages"]
   Get["SamplePackage.m"]

   bruh[3]
(* 10 *)

Nicer way:

  1. Create a new folder SamplePackage inside /Desktop/Mathematica/MyPackages.
  2. Move SamplePackage.m into that new folder.
  3. In /Desktop/Mathematica/MyPackages/SamplePackage create a new subfolder Kernel.
  4. In that new subfolder Kernel, put the following little package init.m whose contents consist of just the single cell: Get["SamplePackage`SamplePackage`"]

Now again append Get["SamplePackage`SamplePackage`"] to $Path and just load the package by:

<< SamplePackage`

Nicest way

Do the same thing as for "Nicer Way" except put the whole folder SamplePackage into the Applications subfolder of $UserBaseDirectory (which under macOS, for example, would be ~/Library/Mathematica/Applications).

This way, you no longer have to use AppendTo[$Path,...]. Now you simple use:

<< SamplePackage`
| improve this answer | |
$\endgroup$
  • $\begingroup$ More generally, AppendTo[$Path, FileNameJoin[{$HomeDirectory, "MyPackages"}]]; $\endgroup$ – flinty Jun 19 at 15:01
  • $\begingroup$ @murray for some reason I can load the files in all three cases you provided but I still cannot use the function bruh[x]...I'm not sure what else I could be doing wrong. Is it a Mathematica error or maybe something I did? Any other ideas? I can provide pictures and video if it helps. $\endgroup$ – Brandon Jun 19 at 18:05
  • $\begingroup$ @Brandon: How do you know you can "load" the packages? What happens if, after doing so, you evaluate ?bruh ? And if you evaluate $ContextPath, is SamplePackage in the list? $\endgroup$ – murray Jun 19 at 18:48
  • $\begingroup$ @Brandon: Another thought: Is the contents of your SamplePackage.m file inside an Initialization cell? See by opening the file in Mathematica and inspect its cell bracket or select the cell bracket and use the menu selection Cell > Cell Properties to see that "Initialization cell" is checked. If not, check that and re-save the file. $\endgroup$ – murray Jun 19 at 18:51
  • $\begingroup$ @Murray Check out the edit I posted. Am I doing something wrong? I must be but like I can't figure it out!!! And I believe it loads it because it echoes nothing, whereas if I specificy a file that either doesn't exist or that isn't in $Path, then I get $Failed, Get: cannot open packagename. I also tried the initialization cell for both the sample package.m and for the notebook I am working in, but it didn't seem to make a difference. $\endgroup$ – Brandon Jun 19 at 19:31

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