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.
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.
- The folder containing the running file
- 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 to split 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
The above line will search for the
numpy module and, if available, store the contents of the module into the variable
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.
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
numpy to allow them to be used in this module.
To import all members of a module you can use the
from numpy import *
The line above will import everything that from numpy, such as
Note: Although this is useful for brevity it is often harder to fix any issues using this method.
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
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.