2
$\begingroup$

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?

$\endgroup$
8
  • $\begingroup$ The package is ok. Is the directory in your $Path? Try Get["testPackage.m", Path -> FileNameJoin[{$UserBaseDirectory, "Applications"}]]. $\endgroup$ Commented Jan 28, 2013 at 22:06
  • $\begingroup$ @ChrisDegnen -- Tried your suggestion. It doesn't work either. Starting to look like a mystery. $\endgroup$
    – Jagra
    Commented Jan 28, 2013 at 22:19
  • $\begingroup$ Maybe Install put it somewhere else. How about copying your package file directly to FileNameJoin[{$UserBaseDirectory, "Applications"}]. $\endgroup$ Commented Jan 28, 2013 at 22:22
  • $\begingroup$ Oops ... old posts coming back to haunt me! Does Get[FileNameJoin[{$UserBaseDirectory, "Applications", "testPackage.m"}]] work? Note that I changed testPackage` to testPackage.m. $\endgroup$
    – Szabolcs
    Commented Jan 28, 2013 at 22:31
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Jan 28, 2013 at 22:35

2 Answers 2

7
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Jagra
    Commented Jan 28, 2013 at 23:08
  • $\begingroup$ I used to create packages with WorkBench and I don't remember needing to initialize cells. $\endgroup$
    – Jagra
    Commented Jan 28, 2013 at 23:11
6
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ +1 especially for the insight into thinking in applications. Thx $\endgroup$
    – Jagra
    Commented Jan 30, 2013 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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