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

@Park, David, I've been trying with no success to list the symbols of a package from within a notebook that Gets the package. I've tried Names[] and ? with various combinations of context labels and quotes. I know the package is working because package functions are evaluated, but I can't get the names. Instead, I typically get an error message: "No symbol matching \!(\"ComplexPackage`*\") found." How do you do it with your project folder structure?

POSTED BY: Gary Palmer
Posted 10 years ago

David's dust story metaphor is right on. There are so many things an inexperienced user can find to do wrong, that finding the one right way to do something can take a long time. In this case, I am happy to report that David's system for managing notebooks and package files in projects is now working for me. I can't say why it didn't work the first time, but it is now working for two test projects, initializing a different package file in each.

In Windows, at least, one can right click in an open Folder and create new files from a New menu. There are many types of files there but only one Wolfram file type, a Wolfram Notebook. Maybe there is some difficulty in it, but it would be really nice if WRI could figure out how to add a Wolfram Package to this menu. This would be the most convenient way to create new packages. (And notebooks also.) One might not even have a convenient Mathematica notebook open to use the Mathematica File/New menu. It's really much more convenient and logical to create new files from the folder that will contain them.

I like that idea, too.

POSTED BY: Gary Palmer

YEA!

I think Wolfram could improve their design and information.

In Windows, at least, one can right click in an open Folder and create new files from a New menu. There are many types of files there but only one Wolfram file type, a Wolfram Notebook. Maybe there is some difficulty in it, but it would be really nice if WRI could figure out how to add a Wolfram Package to this menu. This would be the most convenient way to create new packages. (And notebooks also.) One might not even have a convenient Mathematica notebook open to use the Mathematica File/New menu. It's really much more convenient and logical to create new files from the folder that will contain them.

So I usually end up creating a Mathematica Notebook, changing the suffix to .m and brushing the warning aside. But that creates the package file with the Notebook statement in it, which also needs to be brushed aside. Why is it necessary to put any cell in the new package file? When you create a new notebook it doesn't contain some default cell. Why should a package file. (Users should be aware that a package file can also contain titles, sections and text cells just like a notebook.)

There is also confusion over whether a package file should have a .m or a .wl suffix. Are they completely interchangeable? Will .m be deprecated? I'm not totally certain but I seem to recall that at some point .wl could not be used - I think it may have been as an init.wl file.

All of this creates a little dust storm that envelops the user, especially those new to writing packages. So guys. why don't you fix it and clarify it?

Posted 10 years ago

Many thanks to Eifert and Park for their suggestions. I think I have found the problem, and it's a bit embarrassing. I started reading David Park's tutorial and creating the folder structure he recommends and I started to create what I thought should be in an init file. Previously, when I created a new file for the package, I did not notice the Package option at the bottom of the file menu. I just started with a notebook file and saved it as a .m file. When I opened it, it always came up looking like a notebook file, but on one try it came up gray and I realized that was the format for a package file. (It's been a long time since my last try at packages.) It didn't work, but the entire contents were wrapped in a comment (* contents *), so I removed those and it worked as intended. I will continue to fiddle with this until I'm satisfied or need to return with more questions. I have always gotten great responses on this forum, and this instance is no exception. Big thumbs up.

One little question. Since the init.m file has the m suffix, should it be created with File --> New --> Package?

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
POSTED BY: Gary Palmer
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
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

I"ve sold 100's of applications with packages using this structure and they all have been able to install and use the packages. And palettes and stylesheets automatically appear on their menus and the documentation is accessible. All they had to do was to unzip into their $UserBaseDirectory/Applications folder.

So I'm a bit at a loss why this is not working for you, even with a simpler case.

Mathematica knows where to look because of one of the entries on the Path expression. Is it possible that you have somehow changed your Path definitions? Also in GlobalOptions, File Locations, NotebookPath there is an entry that points to:

FrontEnd`FileName[{$UserBaseDirectory, "Applications", Blank[], 
  "FrontEnd", "Palettes"}]

And on the Path expression there is an entry:

C:\Users\djmpark\AppData\Roaming\Mathematica\Applications

Has one of these somehow gotten changed on your computer? Maybe you could reinitialize your options by holding down Ctrl+Shift when starting Mathematica.

The simplest approach is to give packages a binary name: the application name "PalmerPractice" and the package name "TestPackage". So the BeginPackage statement would be:

BeginPackage[""PalmerPractice`TestPackage`"]

When a user just reads in an application such as:

<< PalmerPractice`

Then it looks to the PalmerPractice/Kernel/init.m file to read in all the packages you might have in the application, and do any other special initializations. That's how it's supposed to work, and how it always has worked for me and people I've sent applications. On Windows and Apple. So it is a bit of a mystery as to what is happening on your system.

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