Importing code from other files

Table of Contents

The importance of importing

The ability to bring in code from other files enables a program to use other code (without requiring that python load up a huge amount of code by default). Below a few scenarios are covered. All of these make use of import.

Importing modules from the Python standard library

Python ships with many modules in its standard library. The following examples show how to access and use a module.

math

math is a module in the standard library that you should check if you're looking for a math-related function. For example, if you wanted to take the square root of a number, math has you covered.

This is how we would import it:

import math

If you are following along in IPython, you can see what is under math by typing math.<TAB>. You will see sqrt there, along with many others.

import math

print(math.sqrt(64))
8.0

Notice that to access something that belongs to the imported module, we need to use a . after the module name.

defaultdict

Another example is defaultdict, which is available in the collections module. Let's see what functionality it adds to normal dictionaries.

We can access defaultdict, which is under collections just as we did sqrt, which is under math.

import collections
# collections.<TAB>

Since collections contains several different data types and we are only going to use defaultdict, we may just want to make that name available directly. We can do that using from.

from collections import defaultdict

Now, let's see why defaultdict is useful. Say we have a DNA sequence and want to get a count of each base type. One way to do that would be to make a dictionary with each base as a key and increment the integer value each time a base is encountered.

sequence = 'ATAGGGCCAAAAAA'

base_counts = {}
for base in sequence:
    base_counts[base] = base_counts.setdefault(base, 0) + 1

print(base_counts)
{'T': 1, 'A': 8, 'C': 2, 'G': 3}

This works, but notice we use setdefault. This says to use the value for the key base if it exists, otherwise initialize the value at 0. Run this code without the setdefault to convince yourself that you would get a key error.

base_counts = {}
for base in sequence:
    # base_counts[base] = base_counts[base] + 1
    base_counts[base] += 1  # Equivalent to the line above

print(base_counts)
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
KeyError: 'A'
{}

defaultdict takes care of this behind the scenes, so that there is no need to use setdefault.

from collections import defaultdict

base_counts = defaultdict(int)
for base in sequence:
    base_counts[base] += 1

print(base_counts)
defaultdict(<class 'int'>, {'T': 1, 'A': 8, 'C': 2, 'G': 3})

Importing code from installed packages

Installed packages are also accessed by importing. These can be packages that you installed with pip or ones from another method, such as an exe installer. SciPy, as its name suggests, is a python package that has many useful tools for science and engineering. Below we will import it and use it to calculate a pearson correlation.

from scipy.stats import pearsonr

Remember, if you're in IPython, you can put ? after the term of interest to get more information on it. Running pearsonr? will display its docstring. We see that it takes two arrays as arguments and returns the pearson correlation and a pvalue. Let's just test it out on inputs that we know should have a correlation of 1.

print(pearsonr([1, 2, 3], [1, 2, 3]))
(1.0, 0.0)

Importing your own code

Another important use of import is accessing code that you've written. This allows you to group related code in one file and access it with many files, without having to duplicate the code in each file.

One example is the codon_to_aminoacid dictionary that is in code/codon_mapping.py. If we wanted to use this dictionary in another file, without copying it over, we could import it.

from codon_mapping import codon_to_aminoacid

Now you can access the variable codon_to_aminoacid just as you would if you created the dictionary within that file. But what if you hate that name for a variable that you are importing? Well, you could of course just assign it a new name after the import.

new_name = codon_to_aminoacid

There is also a way to rename it during the import.

from codon_mapping import codon_to_aminoacid as new_name

Preventing code from running at import

Frequently, you have code that you only want to execute if you run the file from the command line (python <script name>), but not if you import it.

This is done using this special line:

## This code is available after importing this file or when running
## this file from the command line.
def add_two(num):
    return num + 2

if __name__ == '__main__':
    ## This code is only executed when running this file from the
    ## command line.
    print("I'm adding two now.")
    plus2 = add_two(3)

Because we've used the special if __name__... line, we can import add_two from this file into another file (or IPython session) without worrying about the code at the bottom being executed. See code/strjoin.py for an example script that we made in class.

Released under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Created with Emacs 24.4.1 (Org mode 8.3beta)

Validate