Xtralien Scientific Python: Imports

 

When writing programs well organised code can be key to understanding the underlying processes of your code. Because of this it is often useful to split your program across files dependant on the purpose of each part of code.

Structure preface

Before importing a module it is worthwhile knowing how a typical project is structured. When you import a module, Python starts to search in 2 places.

  1. The folder containing the running file
  2. The main Python library directory (this is inside Xtralien Scientific Python)

This search will end as soon as a match for the module is found.

When importing the module any nested import statements will also be run, which is why it can be useful splitting related functions and classes into functions.

common/
    functions.py
    classes.py
main.py

The above directory structure would allow the main body of code to be placed in main.py, which could import common functions from common/functions.py by running import common.functions. This way common.classes is not included, which reduces the size of the program in memory, speeding up the programs execution.

Note: It is also recommended that python files and functions are kept short and concise to enable greater comprehension of their contents and function.

Importing a module

To use another file, or module in your program you need to import your module. Importing a module uses the import keyword.

import numpy

The above line will search for the numpy module and, if available, store the contents of the module into the variable numpy.

Using as

It is possible to set the name of the import when importing by using the as keyword. This is similar to importing into the current file and then assigning the module to a new variable name.

Importing from a module

Most of the time you will not need everything from a module. In these cases you can make use of the from keyword to import a selection of the module directly into the current module.

from numpy import array, random

The line above is relatively simple, and it will load both array and random from numpy to allow them to be used in this module.

Import everything

To import all members of a module you can use the * operator.

from numpy import *

The line above will import everything that from numpy, such as random and array.

Note: Although this is useful for brevity it is often harder to fix any issues using this method.

Import Errors

If the module you want to import does not exist then using import to import it will cause an ImportError to be raised. Using techniques such as the try/except structure it is possible to check for a module and import an alternative if needed.

try:
    import numpy
except ImportError:
import my_numpy as numpy

The above example will attempt to import the numpy module, failing which the my_numpy module will attempt to load. This allows default libraries to be used, enabling greater compatability across different systems.

This is useful if you develop programs that run on different systems, where not all of the optimised libraries may be available. Writing programs this way lets you get the additional performance when you can, and still have a functioning program otherwise.