PE101-01: Using Python Packages and Modules

By itself, Python provides everything you need to write programs. These programs won’t have a fancy user interface and they may not run very fast, but they’ll work. If that’s all Python offered, it might have become a popular language but it wouldn’t have taken over most of the world the way it has. No, what Python has going for it is a simple way to take commonly-used chunks of code, wrap them up neatly into sharable budles, and distribute those bundles far and wide. The mechanism for doing this in Python is called packages.

In this training unit, PE101-01, we’re going to look at some of the packages that come with Python. These are packages that you can count on being available anywhere you can run Python. In the next unit, PE101-02, we’ll look at how to find and use packages hosted in repositories available to anyone but not necessarily already installed where you’re running your programs.

Python is, by itself, a rather simple language. The PE100 series of units has introduced you to almost all of the language. The language is kept small by moving the “nice to have, but not really necessary” parts into their own independent packages. Let’s start with an example:

import math

print("pi equals", math.pi)
print("There are", math.perm(52,2), "possible outcomes when drawing two cards from a deck")
print("The natural logarithm of 7.994 is", math.log(7.994))
pi equals 3.141592653589793
There are 2652 possible outcomes when drawing two cards from a deck
The natural logarithm of 7.994 is 2.0786912602891316

Packages

There are literally oodles of mathematical functions already implemented for you in the math package. To see a list of them as they stand currently, see ” math - mathematical functions ” in the current Python documentation.

Taking a look at the code above, the first thing we notice is the line import math. This tells the Python interpreter to find the package named “math” and to open it up and make its contents available to this session. The things in the package we can get to will all be named by the word “math”, a period, and then the name of the actual part of the package to use. We would say the package “math” is imported into the “math namespace”. This is the default behavior, but we can change that. Indeed:

import random as rand

print(rand.random())
0.5728159131285796

By using the as keyword in our import statement, we’re telling Python to load the “random” package but let us refer to everything as though its name was “rand”. In a little more detail, we’re creating a namespace “rand” instead of just letting Python automatically create a namespace with the same name as the package and load everything into that space.

To see a current list of the packages that come with a standard Python installation, take a look at this comprehensive list. In the first few sections it will list “built-in” capabilities - this is what you can do without importing anything. The rest of the page lists the available packages. Click on any of them for details.

Modules

So far we’ve seen functions and constants placed into packages and directly accessible with just the package name. If you have a large package, or a package that has lots of custom changes to manage, it can be helpful to break things up into modules. Think of a module as a “sub-package”. Package and module names are separated by periods. Let’s take a look…

import os.path as op

if op.exists("/usr/bin"):
    print("all is good.")
else:
    print("I don't even know how the server booted!")
    
all is good.

We imported the “path” module from the “os” package and loaded it into a namespace called “op”. Then we were able to use that namespace to get to the exists() function. We checked to see if the “/usr/bin” directory exists. That is, as you might suspect, a critically important directory.

Coming up next: Packages from the outside world

As we keep saying, one of the biggest (if not the biggest) strengths of Python is the half million packages that people have written and made publicly available. In the next unit, PE101-02: Repositories, Sharing, and Conda, we’ll take a look at how to find those packages, copy them to CHESS servers, and use them in your own notebooks.