Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Using Version 9 on OS X, I've run into some problems with creating packages and accessing them. I've done the following:

  • wrote all the function definitions in a notebook and tested that they all worked;
  • created a package of the definitions.
  • used Install to place the package in the FileNameJoin[{$UserBaseDirectory, "Applications"}] directory;

For good measure I rebooted my computer and restarted Mathematica.

Attempting to load the package gives me nothing, so I simplified everything to see if I'd still have a problem. I made a very simple package:

BeginPackage["testPackage`"]
f::usage = "f[x] returns 2x"
Begin["`Private`"]
f[x_] := 2 x
End[ ]
EndPackage[ ]

I saved and installed it as I described above. Still nothing.

<< testPackage`
Names["testPackage`*"]

{}

I have also tried each of the following:

Get["testPackage`"] (* Doesn't appear to do anything or get any response from Mma *)

Get[FileNameJoin[{$UserBaseDirectory, "Applications", "testPackage`"}]]
$Failed

Needs["testPackage`"]
Needs::nocont: Context testPackage` was not created when Needs was evaluated. >>

Nothing works.

You can see the package installed in the ...Mathematica/Applications directory:

directory

I followed (or at least thought I followed) the recommendations in How to install packages?

I thought doing so would place the package in a path known to the front end.

What have I missed?

share|improve this question
    
The package is ok. Is the directory in your $Path? Try Get["testPackage.m", Path -> FileNameJoin[{$UserBaseDirectory, "Applications"}]]. –  Chris Degnen Jan 28 '13 at 22:06
    
@ChrisDegnen -- Tried your suggestion. It doesn't work either. Starting to look like a mystery. –  Jagra Jan 28 '13 at 22:19
    
Maybe Install put it somewhere else. How about copying your package file directly to FileNameJoin[{$UserBaseDirectory, "Applications"}]. –  Chris Degnen Jan 28 '13 at 22:22
    
Oops ... old posts coming back to haunt me! Does Get[FileNameJoin[{$UserBaseDirectory, "Applications", "testPackage.m"}]] work? Note that I changed testPackage` to testPackage.m. –  Szabolcs Jan 28 '13 at 22:31
    
Also, you installed into $UserBaseDirectory, i.e. choose "Install for this user only", correct? I repeated the exact steps you describe (as far as I can tell), and it works correctly here. The question is what we did differently. –  Szabolcs Jan 28 '13 at 22:35

2 Answers 2

up vote 6 down vote accepted

If you're creating a package using a notebook (with AutoGeneratedPackage -> Automatic) you need to write all your code in "Initialization Cells". Everything else is commented out.

share|improve this answer
    
Terry -- That nailed it. Everything works. Funny because I've poured through the package documentation today and I don't remember any mention of initialization cells. Thx. –  Jagra Jan 28 '13 at 23:08
    
I used to create packages with WorkBench and I don't remember needing to initialize cells. –  Jagra Jan 28 '13 at 23:11

Do not put your code directly in the $UserBaseDirectory/Applications folder. Rather, create a new folder in the Applications folder, MyTestApplication say. Then write your BeginPackage statement as:

BeginPackage["MyTestApplication`testPackage`"]

Load the package with:

<< MyTestApplication`testPackage`

Or, create a Kernel folder in the MyTestApplication folder and create an init.m file in the Kernel folder that contains the above Get statement. Then you can load the package with:

<< MyTestApplication`

Mathematica will execute the init.m file and load the package. You could even have multiple packages and have the init.m file load each of them.

Users seem to constantly stumble over where to put packages. Create an application within the Applications folder and put the package THERE. That's where Mathematica looks for them.

Adding a little more discussion:

It would help if users thought a little less about "Packages" and a little more about "Applications". An application is a folder where you can collect your work on some topic: a book you are studying, or a course you are taking, or a research and development project. Where do you put your application folder? In the Mathematica $UserBaseDirector/Applications folder - where else? Your application folder might or might not contain package files. It may contain many notebooks. It may contain a whole folder structure. If you write packages, which is fairly easy to do, you could also add documentation through Workbench. It is a great way to organize, preserve, document and communicate your work. It is the fruit of your labor.

So, most of all, create those applications folders for your various projects.

share|improve this answer
    
+1 especially for the insight into thinking in applications. Thx –  Jagra Jan 30 '13 at 3:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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