Group Abstract Group Abstract

Message Boards Message Boards

Loading packages in Mathematica 10?

Posted 10 years ago

For years now I have occasionally made use of packages. I have always found that the directions don't work for me, even though I have tried time and again to follow them closely and to try different ways of loading them. I have always had to resort to opening the package and executing it before I could use the package functions in a Mathematica notebook. I am making another try now. The first file is a package. The second file is a notebook that should load the package. It appears that the names of the symbols load into the second script, but they do not function as they should. Any ideas? The following two files should illustrate my problem.

(* filename: Development.m *)

BeginPackage["Development`"]

v::usage = "v[z] converts x+iy to {x, y}."

hello::usage = "Says hello."

Begin["Private`"]

hello = Print["Hello from hello"];

v[z_] := {Re @ z, Im @ z};

End[]

EndPackage[]


(* filename: testPackage.nb *)

SetDirectory[$UserBaseDirectory <> "/Applications"]

(* returns "/Users/myusername/Library/Mathematica/Applications" *)

FileNames[]    (* returns {Development.m} *)

<< Development`

hello        (* returns "hello", which is a failure *)

v[2 + 3 I]  (* returns "v[2+3I]" which is a failure *)

?"Global`*"     (* returns panel with hello and v, but no variables are listed *)
POSTED BY: Gary Palmer
19 Replies
Posted 10 years ago

David,

It works. I had mistakenly substituted notebook folder names for project names.

Gary

POSTED BY: Gary Palmer
Names["PalmerPractice`TestPackage`*"]

Or

?PalmerPractice`TestPackage`*
Posted 10 years ago
POSTED BY: Gary Palmer
Posted 10 years ago
POSTED BY: Gary Palmer

YEA!

Posted 10 years ago
POSTED BY: Gary Palmer

Oh! Well, that totally makes sense, then. I'm almost disappointed, it looked so mystifying... But I'm glad it's working for you now! Yes, you can use File -> New -> Package to create a new package file, or just create a new plain text file from your browser and give it the .m or .wl extension. Package files are just plain text, which I really like: I can read a package file with any old text editor on any device, whether or not Mathematica is installed (heck, even on my phone if I feel like it). And yet, when I do open the file in Mathematica, I get full syntax highlighting and interactive execution and I can just copy & paste code cells from a notebook to a package and vice versa.

By the way, what you did now was create a package that evaluates a Notebook object that then does stuff... It's a bit circuitous, but you know how to change that now.

I won't go into any depth on your answers to my previous diagnostic questions, the answer all look fine now that I know what happened; let me know if something came up in the process that you do want to know more about.

POSTED BY: Bianca Eifert
Posted 10 years ago

Yes, you can use File -> New -> Package to create a new package file, or just create a new plain text file from your browser and give it the .m or .wl extension. Package files are just plain text, which I really like: I can read a package file with any old text editor on any device, whether or not Mathematica is installed (heck, even on my phone if I feel like it). And yet, when I do open the file in Mathematica, I get full syntax highlighting and interactive execution and I can just copy & paste code cells from a notebook to a package and vice versa.

That basic information is very helpful.

POSTED BY: Gary Palmer

Gary,

I have a tutorial on my web site on setting up applications with packages. A Mathematica Style. This is, I believe the standard method WRI has set up for private packages and applications.

I implemented your application and tested that it works. It took about 20 minutes. Here is the file structure used by the application.

$UserBaseDirectory/Applications/
     PalmerPractice/
         Kernel/init.m
         DevelopmentNotebooks/TestNotebook.nb
         TestPackage.m

I've attached the TestPackage.m, TestNotebook.nb and init.m notebooks. Perhaps you could set up the folder structure and then insert the notebooks.

With this structure it is not necessary to use SetDirectory. Mathematica will automatically find everything in the application. You can load the package from anywhere. Even though you have only one package, I set it up so it could accommodate a number of packages. The package is loaded with

<< PalmerPractice`

When Mathematica see that, it automatically reads the init.m file in the application, which does the actual loading of package files.

On my Windows 7, Mathematica 10.4.1 system this all works properly and it should also work on your system.

Attachments:
Posted 10 years ago

David,

I downloaded the files and set up the folder/file tree exactly as you have it. It apparently didn't load the functions because it didn't do anything. It may be a difference between OS X and Windows. I'm not concerned, as I think I can always append to the $Path or set the directory to point to the right places. Just thought you would like to know.

Gary

I've attached the TestPackage.m, TestNotebook.nb and init.m notebooks. Perhaps you could set up the folder structure and then insert the notebooks.

With this structure it is not necessary to use SetDirectory. Mathematica will automatically find everything in the application. You can load the package from anywhere. Even though you have only one package, I set it up so it could accommodate a number of packages. The package is loaded with

<< PalmerPractice`

POSTED BY: Gary Palmer

It should work for both Windows and Apple. Is the TestPackage.m directly in the PalmerPractice folder?

I also want to add that you shouldn't have to add the package to the $Path. Mathematica automatically looks at that location. So I suspect you don't have the correct folder structure.

Posted 10 years ago

David,

I think it is exact as you can see in the attachment. The file init.m is in the Kernel folder.

Gary

Attachments:
POSTED BY: Gary Palmer

I did find an error in the init.m file that I posted. Apologies. it had the statement:

Get["PalmerPractice`TestPAckage`"]

and it should have been:

Get["PalmerPractice`TestPackage`"]

Which makes me wonder why it still worked for me. It appears that on Windows 7 the string in the Get statement is not case sensitive.

Posted 10 years ago

I think you will find this hard to believe, but I changed the capital A in TestPackage to a small a in the init.m file in the Kernel folder and I opened TestNotebook.nb in the DevelopmentNotebooks folder and evaluated the notebook. No results, not even an error message. These things can be hard to find. I had to stare at your message a while before I saw the "A" in TestPAckage.

I don't understand how this is supposed to work. Does MMA have an initialization routine that looks for a Kernel folder in the last folder in the filepath and then finds the init.m file and executes it?

POSTED BY: Gary Palmer
Posted 10 years ago
POSTED BY: Gary Palmer
POSTED BY: Bianca Eifert
Posted 10 years ago
POSTED BY: Gary Palmer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard