XClose

An introduction to research programming with Python

Home
Menu

Installing Libraries

Installing Libraries

We've seen that PyPI carries lots of python libraries. But how do we install them?

In the UCL teaching clusters, we cannot, as these need to be installed by administrators.

So you'll need to follow this lesson on a computer of your own.

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 IPython notebook if you have it open, and reopen it. Check your new library is installed with:

In [1]:
import geopy
geocoder = geopy.geocoders.GoogleV3(domain="maps.google.co.uk")
geocoder.geocode('Cambridge',exactly_one=False)
---------------------------------------------------------------------------
ConfigurationError                        Traceback (most recent call last)
Cell In [1], line 2
      1 import geopy
----> 2 geocoder = geopy.geocoders.GoogleV3(domain="maps.google.co.uk")
      3 geocoder.geocode('Cambridge',exactly_one=False)

File /opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages/geopy/geocoders/google.py:113, in GoogleV3.__init__(self, api_key, domain, scheme, client_id, secret_key, timeout, proxies, user_agent, ssl_context, adapter_factory, channel)
    110 self.secret_key = secret_key
    112 if not self.premier and not api_key:
--> 113     raise ConfigurationError(
    114         'Since July 2018 Google requires each request to have an API key. '
    115         'Pass a valid `api_key` to GoogleV3 geocoder to fix this error. '
    116         'See https://developers.google.com/maps/documentation/geocoding/usage-and-billing'  # noqa
    117     )
    119 self.api_key = api_key
    120 self.domain = domain.strip('/')

ConfigurationError: Since July 2018 Google requires each request to have an API key. Pass a valid `api_key` to GoogleV3 geocoder to fix this error. See https://developers.google.com/maps/documentation/geocoding/usage-and-billing

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

and enter your password.

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)

Ask me over email if you run into trouble.

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?

In [2]:
import numpy
In [3]:
numpy.__path__
Out[3]:
['/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/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.

In [4]:
import sys
sys.path[-4:] # Just list the last few
Out[4]:
['/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8',
 '/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/lib-dynload',
 '',
 '/opt/hostedtoolcache/Python/3.8.14/x64/lib/python3.8/site-packages']

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 .

Make a new folder for your download and unzip the library there.

Now, you need to move so you're inside your download in the terminal:

  • cd my_python_libs
  • cd <library name> (e.g. cd JSAnimation-master)

Now, manually install the library in your PythonPath:

  • python setup.py install

[You might need to do

  • sudo python setup.py install

if you get prompted for 'root' or 'admin' access.]

This is all pretty awkward, but it is worth practicing this stuff, as most of the power of using programming for research resides in all the libraries that are out there.

Finally, writing your own libraries is beyond the scope of this course, but we cover it in Our More Advanced Python Course