Message Boards Message Boards

Loading packages in Mathematica 10?

Posted 8 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 8 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 8 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 8 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 8 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 8 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 8 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 8 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 8 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 8 years ago
POSTED BY: Gary Palmer

That's very strange; there are several things here that shouldn't happen.

First off, your "hello" test definitely won't work the way you expect: You define hello with Set (not SetDelayed), so if everything is working as it should, you'll get the output of hello once during package loading and then never again. If that's not what's happening, then the package wasn't loaded.

Secondly, hello and v live in the "Development" context, they should not show up in "Global" at all.

Could you try the following things and let us know what happens?

First, to find out if the package was loaded at all: Are hello and v marked in blue (i.e. as unknown symbols), or are they black? Did you do all of this on a clean Kernel, or were there possibly definitions of hello and v already floating around? Can you evaluate $ContextPath both before and after trying to load the package (on a fresh Kernel), and let us know what it returns?

Also, can you try the following to see if Mathematica is really finding the correct package file at all? (Maybe there are other packages with the same name higher up on the path.)

FindFile["Development`"]

Does the package work if you put the notebook and the package into the same directory, SetDirectory to that place, and then use Get["Development.m"]? (i.e. load it with its file name rather than the context)

Your question title is specifically about Mathematica 10; did you have this problem with previous versions as well? (You mention having had this problem for years, so I assume it didn't appear with the last upgrade.) Have you experienced the problem on different installs, either when re-installing for some reason or on different computers?

Is this a problem you have with all and any packages, or just your own? Can you use built-in packages? Can you use third-party packages?

Is there any chance that there's something weird in your $UserBaseDirectory/Kernel/init.m file? Does $Path look okay?

(Also, not really a problem, but why do you set your directory to the package directory?)

Edit: Oh, one more minor thing: You forgot a context mark at the beginning of the Private context; it should be:

Begin["`Private`"]

(But your version didn't break the package for me when I just copied and pasted, so it's not the source of your trouble...)

POSTED BY: Bianca Eifert
Posted 8 years ago

My doctor doesn’t ask that many questions, but then maybe Mathematica is more complicated than I am. But I’m happy to try to answer them and they are instructive to me.

Hello and v show up in blue. When I click on them, the panel displays “Globalv” and “Globalhello”. I turned on the computer this morning and running the test program was my first act in Mathematica. Oddly, there are a couple of other symbols listed that appear to be from some previous program. This is recurrent. One of them appears to be black, though it is hard to be sure.

I added the “`” prefix to “Private” in Development.m, quit MMA, restarted and inserted two instances of $ContextPath in the following order:

$ContextPath

<< Development`

$ContextPath

The output was:

{"TemplatingLoader", "PacletManager", "System", "Global"}

{"TemplatingLoader", "PacletManager", "System", "Global"}

Other output was unchanged.

FindFile["Development`"] returns

/Users/username/Library/Mathematica/Applications/Development.m

Development.m was installed with File —> Install….

You wrote: “Does the package work if you put the notebook and the package into the same directory, SetDirectory to that place, and then use Get["Development.m"]? (i.e. load it with its file name rather than the context)”

I made a copy of Development.m from the above Applications folder and pasted it into my working folder with test_package.nb. I added a call to FileNames[]. The behavior in a clean kernel was exactly as before. A call to FileNames[] showed the files in this working folder.

I have had MMA 10.0.0.0 for several years, I think, and have not had occasion to reinstall that I can remember. I don’t remember if I had the same problem with packages prior to MMA 10. It is installed on OS 10.9.5. I have never tried third party packages and I haven’t had occasion to load any built-in packages. I’m definitely not what one would call a Mathematica expert.

You asked: “why do you set your directory to the package directory?”

Because I had tried appending the file path to $Path. The append was successful, but the result was the same. I wanted to be absolutely sure that the package was found. It seems the package is being found, but there is some problem in loading the contents.

You asked: Is there any chance that there's something weird in your $UserBaseDirectory/Kernel/init.m file? Does $Path look okay?

$Path

{"/Applications/Mathematica.app/SystemFiles/Links", \ "/Users/username/Library/Mathematica/Kernel", \ "/Users/username/Library/Mathematica/Autoload", \ "/Users/username/Library/Mathematica/Applications", \ "/Library/Mathematica/Kernel", "/Library/Mathematica/Autoload", \ "/Library/Mathematica/Applications", ".", "/Users/username", \ "/Applications/Mathematica.app/AddOns/Packages", \ "/Applications/Mathematica.app/AddOns/LegacyPackages", \ "/Applications/Mathematica.app/SystemFiles/Autoload", \ "/Applications/Mathematica.app/AddOns/Autoload", \ "/Applications/Mathematica.app/AddOns/Applications", \ "/Applications/Mathematica.app/AddOns/ExtraPackages", \ "/Applications/Mathematica.app/SystemFiles/Kernel/Packages", \ "/Applications/Mathematica.app/Documentation/English/System", \ "/Applications/Mathematica.app/SystemFiles/Data/ICC"}

When I attempt to display init.m with “more” in the Terminal program, I see only:

(** User Mathematica initialization file **)

Thanks for the interest. Now on to the next reply from David.

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

Group Abstract Group Abstract