Skip main navigation

Using libraries

An introduction to using libraries in Python

In this article we give a quick overview of how to go about using Python libraries, packages and modules in your code.

You might hear the terms library, package and module used interchangably when referring to bits of Python code, and really the only difference is in the amount of code and the way it is organised. A single chunk of Python code in a single file is referred to as a module, while a set of modules grouped together is often referred to as a package, while a set of packages grouped together is often called a library.

Python Namespace and importing

Once you’ve installed a new library or package, to use it in your code you’ll first have to import it. You do this within the code itself in one of a few ways.

An important concept here is that of the Python namespace. Put simply, this is a list of words that Python can understand when you put them in your code. All the functions in the standard library are automatically in the namespace so you can just use them without importing them (e.g. print, open), but functions from installed packages are not. To use them you need to add either the name of the package or the name of specific functions within the package to the namespace.

So for example, if you want to calculate the square root of a number using the square root function from numpy, sqrt, you can first add the name of the package numpy to the namespace using the import statement as follows:

import numpy

This only adds the name numpy, not the name of the sqrt function. To use that you have to refer to it with the prefix numpy, then a fullstop character. So for example, to get the square root of 2 we need to use the following:

numpy.sqrt(2)

To save you having to type the package name out every time you use a function you can use the from statement to identify the package then import the function name itself. In our example this would be:

from numpy import sqrt

Now we can just use sqrt without typing out numpy. first as follows:

sqrt(2)

You can also add multiple functions to the namespace at once using a comma separated list e.g.:

from numpy import sqrt, array, sin, cos

Will add the square root function as before but also the function to make an array, and the trigonometric functions sin and cos.

Renaming packages and functions

Often, rather than importing lots of functions from a package individually, programmers will instead import the package name as we did at first but rename with an abbreviated version using the ‘as’ command. A common abbreviation used for numpy is np, so you might often see:

import numpy as np

Then to use the functions you need to use the abbreviated form:

np.sqrt(2)

You can even rename functions using the ‘as’ command:

from numpy import sqrt as squareroot

Then just use the new name instead:

squareroot(2)

The main reason for doing this is that you might have two functions from separate packages with exactly the same name. If you try and import both names directly into the namespace Python will just overwrite the first name imported with the most recently imported version. So if you have two packages ‘package1’ and ‘package2’ that both have functions called ‘do_something’ the following code:

from package1 import do_something
from package2 import do_something

would first import the do_something function from package1, then overwrite it with do_something from package2, which might well do something completely different. So instead you might use the following:

from package1 import do_something
from package2 import do_something as do_something_else

And use either do_something or do_something_else as and when you needed them.

To be less ambiguous it might be better here to just import the package names:

import package1, package2

And then use:

package1.do_something()
package2.do_something()

when you needed to use them.

Its important to be careful with the name you choose if you rename functions. While all the functions in the standard library are automatically imported into the namespace, you can still overwrite their names. As an example, though its unlikely you would want to do this, the following code works perfectly well:

from numpy import sqrt as print
print(2)

but gives the print function very different behaviour than usual (try this out – what do you predict will happen?).

You may also see the wildcard character * used to import functions e.g.:

from numpy import *

Basically this means ‘from numpy import everything you can’. Though this can save time if you’re importing a lot of functions from one package, its generally not good practice since as discussed above, if you’re using several packages at once its possible to overwrite names in the namespace without realising it, leading to ambiguity in exactly what function from what package you are using at a given time.

Summary

That’s it for getting ready to use Python! It’s been a lot to get through, and you may still have some questions, but hopefully next week when we start some coding properly, you will be set up ready. We will even begin by coding inside Fiji so if you’re struggling to set up Python don’t worry too much for now – Fiji can be used to code Python on it’s own, as we will see.

To finish off this week, we will give you some Python code to make a spreadsheet from an image, as we saw earlier.

This article is from the free online

Introduction to Image Analysis for Plant Phenotyping

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now