Using Libraries

To use a function or type from a python library, rather than a built-in function or type, we have to import the library.

math.sin(1.6)


—————————————————————————

NameError Traceback (most recent call last)

in () </span>

—-> 1 math.sin(1.6)

NameError: name ‘math’ is not defined

Importing Libraries

import math

math.sin(1.6)


0.9995736030415051

We call the libraries modules:

type(math)


module

The tools supplied by a module are attributes of the module, and as such, are accessed with a dot.

dir(math)


[‘doc’,

file’,

name’,

package’,

spec’,

‘acos’,

‘acosh’,

‘asin’,

‘asinh’,

‘atan’,

‘atan2’,

‘atanh’,

‘ceil’,

‘copysign’,

‘cos’,

‘cosh’,

‘degrees’,

‘e’,

‘erf’,

‘erfc’,

‘exp’,

‘expm1’,

‘fabs’,

‘factorial’,

‘floor’,

‘fmod’,

‘frexp’,

‘fsum’,

‘gamma’,

‘gcd’,

‘hypot’,

‘inf’,

‘isclose’,

‘isfinite’,

‘isinf’,

‘isnan’,

‘ldexp’,

‘lgamma’,

‘log’,

‘log10’,

‘log1p’,

‘log2’,

‘modf’,

‘nan’,

‘pi’,

‘pow’,

‘sin’,

‘sinh’,

‘sqrt’,

‘tan’,

‘tanh’,

‘trunc’]

They include properties as well as functions:

math.pi


3.141592653589793

You can always find out where on your storage medium a library has been imported from:

print(math.__file__[0:50])
print(math.__file__[50:])


/usr/local/Cellar/python3/3.5.2_1/Frameworks/Pytho

Note that ‘import’ does not install libraries. It just makes them available to your current notebook session, assuming they are already installed. Installing libraries is harder, and we’ll cover it later. So what libraries are available? Until you install more, you might have just the modules that come with Python, the standard library.

Review the list of standard library modules: https://docs.python.org/library/

If you installed via Anaconda, then you also have access to a bunch of modules that are commonly used in research.

Supplementary Materials: Review the list of modules that are packaged with Anaconda by default on different architectures: https://docs.anaconda.com/anaconda/packages/pkg-docs/ (modules installed by default are shown with ticks)

We’ll see later how to add more libraries to our setup.

Why bother?

Why bother with modules? Why not just have everything available all the time?

The answer is that there are only so many names available! Without a module system, every time I made a variable whose name matched a function in a library, I’d lose access to it. In the past, people ended up having to make really long variable names, thinking their names would be unique, and they still ended up with “name clashes”. The module mechanism avoids this.

Importing from modules

Still, it can be annoying to have to write ‘math.sin(math.pi)’ instead of ‘sin(pi)’.

Things can be imported from modules to become part of the current module:

import math
math.sin(math.pi)


1.2246467991473532e-16

from math import sin
sin(math.pi)


1.2246467991473532e-16

Importing one-by-one like this is a nice compromise between typing and risk of name clashes.

It is possible to import everything from a module, but you risk name clashes.

from math import *
sin(pi)


1.2246467991473532e-16

Import and rename

You can rename things as you import them to avoid clashes or for typing convenience.

import math as m
m.cos(0)


1.0

pi = 3
from math import pi as realpi
print(sin(pi), sin(realpi))


0.1411200080598672 1.2246467991473532e-16

Installing Libraries

We’ve seen that PyPI carries lots of python libraries, but sometimes we need additional libraries.

How do we install them?

For UCL colleagues working a cluster room, we cannot, as these need to be installed by administrators.

For this lesson you will need to use a computer of your own or one on which you have administrator rights.

The main problem is this: libraries need other libraries

So you can’t just install a library by copying code to the computer: you’ll find yourself wandering down a tree of “dependencies”; libraries needed by libraries needed by the library you want.

This is actually a good thing; it means that people are making use of each others’ code. There’s a real problem in scientific programming, of people who think they’re really clever writing their own twenty-fifth version of the same thing.

So using other people’s libraries is good.

Why don’t we do it more? Because it can often be quite difficult to install other peoples’ libraries!

Python has developed a good tool for avoiding this: pip.

Installing Geopy using Pip

On a computer you control, on which you have installed python via Anaconda, you will need to open a terminal to invoke the library-installer program, pip.

• On windows, go to start->all programs->Anaconda->Anaconda Command Prompt
• On mac, start terminal.
• On linux, open a bash shell.

Into this shell, type:

pip install geopy

The computer will install the package automatically from PyPI.

Now, close Jupyter notebook if you have it open, and reopen it.

Check your new library is installed with:

import geopy
geocoder.geocode('Cambridge',exactly_one=False)


[Location(Cambridge, UK, (52.205337, 0.121817, 0.0))]

That was actually pretty easy, I hope. This is how you’ll install new libraries when you need them.

Troubleshooting:

On mac or linux, you might get a complaint that you need “superuser”, “root”, or “administrator” access. If so type:

• sudo pip install geopy

If you get a complaint like: ‘pip is not recognized as an internal or external command’, try the following:

• conda install pip (Windows)
• sudo easy_install pip (Mac, Linux)

For those of you that are UCL colleagues, we recommend that you attend a Drop-In Session

Installing binary dependencies with Conda

pip is the usual Python tool for installing libraries. But there’s one area of library installation that is still awkward: some python libraries depend not on other python libraries, but on libraries in C++ or Fortran.

This can cause you to run into difficulties installing some libraries. Fortunately, for lots of these, Continuum, the makers of Anaconda, provide a carefully managed set of scripts for installing these awkward non-python libraries too. You can do this with the conda command line tool, if you’re using Anaconda.

Simply type

• conda install <whatever>

instead of pip install. This will fetch the python package not from PyPI, but from Anaconda’s distribution for your platform, and manage any non-python dependencies too.

Typically, if you’re using Anaconda, whenever you come across a python package you want, you should check if Anaconda package it first using this list: http://docs.continuum.io/anaconda/pkg-docs.html. (Or just by trying conda install and hoping!) If you can conda install it, you’ll likely have less problems. But Continuum don’t package everything, so you’ll need to pip install from time to time.

Where do these libraries go?

import numpy

numpy.__path__


[‘/home/travis/virtualenv/python3.5.2/lib/python3.5/site-packages/numpy’]

Your computer will be configured to keep installed Python packages in a particular place.

Python knows where to look for possible library installations in a list of places, called the “PythonPath”.

It will try each of these places in turn, until it finds a matching library name.

import sys
sys.path[-4:] # Just list the last few


[‘/opt/python/3.5.2/lib/python3.5/plat-linux’,

‘/home/travis/virtualenv/python3.5.2/lib/python3.5/site-packages’,

‘/home/travis/virtualenv/python3.5.2/lib/python3.5/site-packages/IPython/extensions’,

‘/home/travis/.ipython’]

Libraries not in PyPI

Sometimes, such as for the Animation library in the Boids example, you’ll need to download the source code directly. This won’t automatically follow the dependency tree, but for simple standalone libraries, is sometimes necessary.

To install these on Windows, download and unzip the library into a folder of your choice, e.g. my_python_libs.

On Windows, a reasonable choice is the folder you end up in when you open the Anaconda terminal.

You can get a graphical view on this folder by typing: explorer .

• cd my_python_libs
• cd <library name> (e.g. cd JSAnimation-master)
• python setup.py install
[You might need to do sudo python setup.py install if you get prompted for ‘root’ or ‘admin’ access.]