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 "MyFormat1" are explicitly evaluated.

Click for copyable input
Click for copyable input


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 ImportExport`RegisterImport[] code block into a file named Import.m.

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

Click for copyable input

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

For example, in the directory $UserBaseDirectory/SystemFiles/Formats/MyFormat1, the file Converter.m 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 "MyFormat1", 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/SystemFiles/Formats.

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

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 "Sources" option. For example, "Sources"->{"MyFormatConverter/Converter.m","MyFormatConverter/Converter2.m" }.

Resolving Conflicts

In case of conflicts (for example, the "MyFormat" 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 "MyOtherFormat" and "MyDefaultFormat" as aliases of "MyFormat1". 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:
Click for copyable input
The original format returns the same information:
Click for copyable input