Importing Modules#
Prerequisites#
Learning Outcomes#
Importing a function
Importing a module
Importing a library
What are modules and libraries?#
A module is a file consisting of Python code. A library is a collection of modules.
Modules can define functions, variables, and classes, and can include runable code. Any Python file can be referenced as a module.
This is extremely useful, as it means you do not have to write out complex functions in your program itself. You can store them externally, and just import them for use in your program.
Accessing a module or library#
In order for your program to be able to access a module or library, they must be in an accessible location.
Built-in Python modules include math, statistics and time, and will be accessible as long as you have Python fully installed.
If you are using an environment such as Anaconda, many common libraries will already be accessible. However, if you want to access more niche libraries such as RDKit, you will have to add this to your environment. You can usually do this by writing conda install module_name to the command line, where module_name is the name of the library or module you want to add.
Importing a module or library in your code#
Often, we wish to use pre-written libraries, for example the NumPy library for data analysis. To do this we use the import command.
# Import numpy into the code
import numpy
# Import numpy and give it an alias
import numpy as np
# Import the whole math module
import math
# Import just the square root function
from math import sqrt
You then call the function from the library you imported by typing the library name (or alias) dot the function name.
# Import math and use the function sqrt
import math
answer = math.sqrt(4)
Using aliases#
An alias is a name that you give to an imported library or module that you can use to refer to it throughout your code. This is very useful when your libraries or modules have long names. You do this using the keyword as. For example:
import module as mod
You will notice that aliases are very consistent across institutions, making it very easy to understand code that other people have written. Each library/module has recommended aliases to help consistency.
Some common aliases include:
# The library NumPy is for advanced mathematics
import numpy as np
# The library Matplotlib is for visualisations
import matplotlib as mpl
# The module Pyplot (from Matplotlib) creates plots, graphs and charts
from matplotlib import pyplot as plt
# The library pandas is for data analysis and manipulation
import pandas as pd
For some modules, it is recommended not to use an alias, as shortening some names can lead to a name clash. This is when multiple variables, functions, or modules have the same name. This could happen if you gave your module a single-letter name, for example import math as m.
Using import *#
in some tutorials it will be suggested you use the
from [] import *
this is bad practise as it imports everything and removes the need to reference the function using the dot notation above. This can lead to name clashes between functions and unexpected behaviour.
we can also import our own modules into our code. This can lead to much simpler code.
If we have a python script file called my_funcs.py
We can import any functions in this script using the following
import my_funcs as mine
Summary#
A module is a file consisting of Python code, and a library is a collection of modules.
Import libraries and build-in modules using the syntax
import name, for exampleimport math.Libraries and modules must be accessible by your code. If you use an environment like Anaconda, many will already be supported, however others may need to be installed via your environment.
Import functions using
from, for examplefrom math import cos.Import modules from libraries using
from, for examplefrom matplotlib import pyplot.Assign an alias which you can use to refer to your module name rather than writing it out using the keyword
as. Common aliases are:import numpy as npimport matplotlib as mplfrom matplotlib import pyplot as pltimport pandas as pd