XClose

MPHY0021: Research Software Engineering With Python

Home
Menu

Packaging your code

Installing Libraries

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

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 -> Anaconda3 -> Anaconda Prompt
  • On mac, start Terminal.
  • On linux, open a bash shell.

Into this shell, type:

pip install geopy

The computer will install the package and its dependencies automatically from PyPI (a repository of packages, which we'll talk about later).

Now, close the Jupyter notebook if you have it open, and reopen it. Check your new library is installed with:

In [1]:
import geopy
geocoder = geopy.geocoders.Nominatim(user_agent="mphy0021") 
In [2]:
geocoder.geocode('Cambridge', exactly_one=False)
Out[2]:
[Location(Cambridge, Cambridgeshire, East of England, England, United Kingdom, (52.2055314, 0.1186637, 0.0)),
 Location(Cambridge, Middlesex County, Massachusetts, United States, (42.3750997, -71.1056157, 0.0)),
 Location(Cambridge, Region of Waterloo, Southwestern Ontario, Ontario, Canada, (43.3600536, -80.3123023, 0.0)),
 Location(Cambridge, Henry County, Illinois, United States, (41.3036472, -90.1928971, 0.0)),
 Location(Cambridge, Isanti County, Minnesota, United States, (45.5727408, -93.2243921, 0.0)),
 Location(Cambridge, Story County, Iowa, 50046, United States, (41.8990768, -93.5294029, 0.0)),
 Location(Cambridge, Dorchester County, Maryland, 21613, United States, (38.5714624, -76.0763177, 0.0)),
 Location(Cambridge, Guernsey County, Ohio, United States, (40.031183, -81.5884561, 0.0)),
 Location(Cambridge, Jefferson County, Kentucky, United States, (38.2217369, -85.616627, 0.0)),
 Location(Cambridge, Cowley County, Kansas, United States, (37.316988, -96.66633224663678, 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:

  • pip install --user geopy

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

  • conda install pip (if you are using Anaconda - though it should be already available)
  • or follow the official instructions otherwise.

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 conda search. If it is there you can conda install it, you'll likely have less problems. But Anaconda doesn't package everything, so you'll need to pip install from time to time.

The maintainers of packages may have also provided releases of their software via conda-forge, a community-driven project that provides a collection of packages for the anaconda environment. In such case you can add conda-forge to your anaconda installation and use search and install as explained above.

Where do these libraries go?

In [3]:
geopy.__path__
Out[3]:
['/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/geopy']

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 (%PYTHONPATH% in Windows). It will try each of these places in turn, until it finds a matching library name.

In [4]:
import sys
sys.path
Out[4]:
['/home/runner/work/rsd-engineeringcourse/rsd-engineeringcourse/ch04packaging',
 '/opt/hostedtoolcache/Python/3.8.12/x64/lib/python38.zip',
 '/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8',
 '/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/lib-dynload',
 '',
 '/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages',
 '/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/extensions',
 '/home/runner/.ipython']

You can add (append) more paths to this list, and so allow libraries to be load from there. Thought this is not a recommended practice, let's do it once to understand how the import works.

  1. Create a new directory (e.g., myexemplar),
  2. create a file inside that directory (exemplar.py),
  3. write a function inside such file (exemplar_works),
  4. open python, import sys and add the path of myexemplar to sys.path,
  5. import your new file, and
  6. run the function.

Libraries not in PyPI

Sometimes 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:

  • pip install --user .

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

Python virtual environments

Sometimes you need to have different versions of a package installed, or you would like to install a set of libraries that you don't want to affect the rest of the installation in your system. In such cases you can create environments that are isolated from the rest.

There are multiple solutions to this, only for python or for anaconda. Find more information on how to create and use the virtual enviroments.