What Is a Wolfram Language Package?


Introduction

A Wolfram Language package is used to store Wolfram Language code so that it can be loaded into a Wolfram Engine session. Typically, a package is placed in a file that has the extension ".m".

A Wolfram Language package provides one or more functions, which are placed into a context or group of Wolfram Language symbols. The code that gives the functionality is hidden in an implementation section of the package.

Structure of a Package

The contents of a sample package are shown below.

  BeginPackage[ "Package`"]

  MainFunction::usage = 
	"MainFunction[ x] computes a simple function."

  Begin[ "`Private`"]

  MainFunction[ x_] :=
    Module[ {y},
      y = x^2;
      y + 1
    ]

  End[]

  EndPackage[]

The context for the package is Package`; this is specified by the argument to BeginPackage. The package exports one function named MainFunction; this is the only function declared between the BeginPackage and the Begin. All the code used to implement the package is kept in the Private section of the package; this is between the Begin and the End statements.

Loading a Package into the Wolfram Engine

To load a package into the Wolfram Engine the package needs to be placed into a directory on the Wolfram Engine $Path. After this it can be loaded with either Get (often this entered with the '<<' syntax) or Needs.

  In[1]:= << Package`

  In[1]:= MyFunction[ 10]

  Out[1]= 101

If the package cannot be loaded then the Wolfram Engine will print an error. This will happen if the package has not been placed on the Wolfram Language $Path. In this case you might want to search for the package, for example, using the command FindFile.

  In[2]:= FindFile[ "Package`"]

  Out[2]= "C:\Documents and Settings\User\My Documents\work\Eclipse_workspaces\Demo1\Package.m"

If you load a package with Needs, it will only be loaded if it has not been loaded before. If you load a package with Get, it will always be loaded. It is a useful optimization feature to only load a package once. However, when you are developing the package, it is often useful to load it every time you make a change, and so you should use Get.

Package Dependencies

One of the important techniques for building software applications is to break up your code into different components, each of which does different things. You can then set your package to load code from other packages. There are two ways that you can do this. In the first example, the extra packages are placed in the BeginPackage command. This makes all the functions in Package1` and Package2` available to the Wolfram Language session as well as to the functions inside Package`.

  BeginPackage[ "Package`", {"Package1`", "Package2`"}]
  ...

The second technique for loading package dependencies uses Needs just after the BeginPackage command. If you do this, then the Wolfram Language session is not affected, but functions in Package` can use the imported commands.

  BeginPackage[ "Package`"]
  Needs[ "Package1`"]
  Needs[ "Package2`"]
  ...

Summary

Wolfram Language packages are an important way to write and deliver code. Properly structured packages help to ensure that multiple packages can be installed and run within the Wolfram Language without their interfering with each other. Consequently, the Workbench provides a lot of support for working with packages.