OOF2: The Manual

7.4. Running OOF2 with External Extensions

It is first necessary to tell Python how to find the extension, and then to tell OOF2 to load it.

7.4.1. Setting PYTHONPATH for Extensions

For an extension to be used by OOF2 it must be located in a directory that is in Python's path — the list of directories that Python searches when it imports a module. You can check the path by running

    python -c "import sys; print(sys.path)" 

in a terminal window.

If the directory containing the extension is not listed, there are a number of ways to add it. In all of these examples, replace the word directory with the file system path to the directory containing the extension module.

  1. Set the PYTHONPATH environment variable. The way that is done depends on which version of the Unix shell you're using. In bash, for example, you would type

        export PYTHONPATH=$PYTHONPATH:directory 

    to append the directory to an existing path. This will apply to all future OOF2 sessions and any other Python programs started in the current terminal window. To make this change apply to all future terminal windows, put the export line in your shell's start-up file (~/.profile in bash).

  2. Start OOF2 with the --pathdir=directory option, which will add "directory" to the path just for that session of OOF2 and won't affect any other Python programs. Equivalently, in bash and maybe other shells, you can start OOF2 like this:

        PYTHONPATH=directory oof2 

  3. To make the change for all future OOF2 sessions, but not affect any other Python programs, insert the line

        sys.path.append("directory") 

    in the .oof2rc file in your home directory.

7.4.2. Loading External Extensions

There are a few different ways of loading external extension modules. They all rely on having the extension's installation directory in the Python path, as explained in Section 7.4.1. The main differences between the methods are

  • Whether they load the module when OOF2 starts or later on in the session.

  • Whether the loading is done by an OOF2 menu command or directly by Python. If it's loaded by a command, the command will be saved in the session log, and the module will be reloaded if the log is reloaded.

  • Whether they apply to all future OOF2 sessions or just the current one.

The list below contains all the options. In each example, replace oofextension with the name of your extension:

  1. Run the command OOF.File.Load.Module from the File menu in the main OOF2 window, and type the module name in the dialog box. This will load the module using a scriptable command in the current OOF2 session.

  2. Put the line

        OOF.File.Load.Module(module="oofextension") 

    in the .oof2rc in your home directory. This will load the module using a scriptable menu command in all future OOF2 sessions.

  3. Start OOF2 with the --import option, like this:

        oof2 --import=oofextension 

    This loads the module at start-up time without using a menu command, and works only on the current session. Modules loaded with --import are loaded before the .oof2rc file is read, so this method won't work if the path to the module is set in .oof2rc. If you need to adjust the path at run time, you can use --pathdir in conjunction with --import:

        oof2 --pathdir=directory --import=oofextension 

  4. Type the line

        import oofextension 

    in the Console Window during an OOF2 session. This loads the module for the current session without using a menu command.

  5. Put the line

        import oofextension 

    in the .oof2rc in your home directory. This will load the module without using a menu command in all future OOF2 sessions.

It is permissable to import a module more than once and by more than one method. Only the first import will have an effect, though.