WOLFRAM LANGUAGE TUTORIAL

Automatic Loading of Import/Export Converters

In addition to the interfaces of Import and Export, the Wolfram Language comes with an autoloading mechanism that simplifies the registration of formats used by the Import and Export framework.

Autoloading versus Manual Loading

Manual Loading

Examples of manually loading converters are given in the tutorials, where the commands to register are explicitly evaluated.

Click for copyable input
Click for copyable input

Autoloading

By placing both the format registration and the converter in appropriate locations, the Wolfram Language can automatically load the format registration. Autoloading of Import and Export converters can be achieved by following these steps:

(In the steps below, you can use $UserBaseDirectory or $BaseDirectory.)

1. If the directory $UserBaseDirectory/SystemFiles/Formats does not exist, create it.

2. Under $UserBaseDirectory/SystemFiles/Formats, create a directory with the name format, where format is the name of the format to be autoloaded.

3. Inside the format directory, place the code block into a file named .

For example, in the directory $UserBaseDirectory/SystemFiles/Formats/MyFormat1, the file would have the following content.

Click for copyable input

4. Inside the format directory, place the converter source code into a file named .

For example, in the directory $UserBaseDirectory/SystemFiles/Formats/MyFormat1, the file would have the following content.

Click for copyable input

5. With the files in place, when the Wolfram Language starts, $ImportFormats will contain the entry , and Import[ file,{"MyFormat1","Header"}] will evaluate appropriately.

In fact, all formats supported by the Wolfram Language are registered using this autoloading mechanism, with the format registration residing in $InstallationDirectory.

Furthermore, several formats in the Wolfram Language are made available in source form in the directory $InstallationDirectory.

Details of the Registration Mechanism

Directory Name Must Match the Format Name

When using the autoloading mechanism, the folder name must match the format name. Formats can effectively be renamed by using format aliases below.

"Sources" Must Be Explicitly Given If Source Files Are Not in Standard Locations

When using the autoloading mechanism, the Import/Export framework assumes the source file to be located in either $UserBaseDirectory/SystemFiles/Formats/format/Converter.m or $BaseDirectory/SystemFiles/Formats/format/Converter.m. If the source files are located elsewhere, it must be explicitly declared using the option. For example, .

Resolving Conflicts

In case of conflicts (for example, the directory residing in both $InstallationDirectory/SystemFiles/Formats and $UserBaseDirectory/SystemFiles/Formats), the registration in $UserBaseDirectory has highest priority, followed by $BaseDirectory and $InstallationDirectory.

Defining Format Aliases

It is often useful to define format aliases so that you may refer to a format with several different names. Suppose you would like to use and as aliases of . You can do this by creating the file $UserBaseDirectory/SystemFiles/Formats/FormatMappings.m with the following contents.

Click for copyable input
The aliases work the same as the original format.
In[5]:=
Click for copyable input
Out[5]=
The original format returns the same information.
In[7]:=
Click for copyable input
Out[7]=