1 Before you start
This is not graded, and it does not count towards anything. Answer every question even if you are only guessing — having a go at something before you have been taught it is one of the best ways to remember it afterwards. The lesson ends with another short check, and there you will be shown the correct answer for anything you missed. Choose an option for every question and confidence rating, then click Submit answers.
You write import math at the top of your script. Which of these calls works as expected?
After the line from statistics import mean, how do you call the function?
What does the keyword as do in import numpy as np?
Why is from math import * discouraged?
Which of these must you pip install before you can import it?
A script begins with import biopython. You run it and get ModuleNotFoundError: No module named 'biopython'. What is the right next step?
Which command lists every Python package currently installed in your environment?
What is a requirements.txt file used for?
I can import a built-in module like math or statistics and call one of its functions using the module-name prefix.
I can install a third-party package with pip from a terminal and recognise the ModuleNotFoundError that says I need to.
I can read a short script’s import block and tell a peer which modules are built in, which are third-party aliases, and what each module is being used for.
2 Introduction
Almost nothing you write in precision medicine is written from scratch. When you need an average, you do not write your own function. You use code that other people have already written, tested and shared.
This lesson covers two ways to bring code into a script: loading code that comes with Python, and installing code that does not. It also shows what to do when Python reports a module is missing.

Try the examples in the Python Scratchpad on the right.
3 Importing built-in modules
Suppose you have five potassium results, in mmol/L, and you want the median. The median is the middle value once the results are in order. You could write this code yourself. But other people have already written, tested and shared it. Python comes with a large collection of such code. So there is nothing extra to download. This collection is called the standard library. The code in the standard library is organised into modules.
3.1 Import modules
A module is a file of Python code holding functions on one topic. For example, the statistics module holds functions that describe a list of numbers. A built-in module comes with Python. So you do not need to download or install it.
To use a module, ask Python to load it. Write the keyword import, then the module name, on a line near the top of your script. Here is a script that loads statistics and finds the median of the five results:
import statistics
levels = [4.6, 3.9, 5.8, 5.2, 6.1]
print(statistics.median(levels)) # 5.2The first line loads the module statistics. The call statistics.median(levels) runs the function median from that module. It returns the middle value, 5.2. Type the code below into the Python Scratchpad and run it.
Try this snippet in the Python Scratchpad on the right.
import math
result = math.sqrt(16)
print(result)
The line import statistics gives your script one new name, statistics. This name stands for the whole module, with every function inside it. To call one of these functions, write the module name, a dot and the function name. The dot tells Python to look inside the module.
You need the module name in every call. This is because the line gave your script only the name statistics. The name median exists only inside the module. So this call fails:
import statistics
levels = [4.6, 3.9, 5.8, 5.2, 6.1]
print(median(levels))
# NameError: name 'median' is not definedEach call starts with its module name. The part before the dot is called the prefix. The prefix tells the reader which module a function came from. This matters when a script loads more than one module. In the next example, datetime.date makes a date from a year, month and day:
import statistics
import datetime
levels = [4.6, 3.9, 5.8, 5.2, 6.1]
print(statistics.median(levels)) # 5.2
print(datetime.date(2026, 10, 22)) # 2026-10-22
You write the line loading a module once, near the top of the script. The module stays loaded until the script ends.
The standard library has many more modules. For example, random picks items by chance, csv reads and writes files of comma separated values, and os talks to the operating system.
You do not need to memorise this list. When you need a tool for a common task, search for “python standard library” and the task before you write the code yourself.
3.2 Other shapes of import
There are three other ways to write the line that loads a module. So far we have used the plain form, import statistics. The plain form keeps the module name as a prefix on every call.
The first way uses the keyword from. The line from module import name takes one name from the module and puts it directly into your script. So you use that name with no prefix.
from datetime import date
print(date(2026, 10, 22)) # 2026-10-22The call date(2026, 10, 22) has no prefix. The line from datetime import date did not load the name datetime. So the longer form datetime.date does not work in this script:
from datetime import date
print(datetime.date(2026, 10, 22))
# NameError: name 'datetime' is not defined. Did you forget to import 'datetime'?The from form helps when you need only one or two names from a module. The cost is that a reader has to remember or guess which module a name came from.
The second way uses the keyword as. The line import module as alias loads the whole module under a shorter name that you choose. This name is called an alias.
import statistics as stats
levels = [4.6, 3.9, 5.8, 5.2, 6.1]
print(stats.median(levels)) # 5.2After that line, your script has the name stats. Your script does not have the name statistics. The keyword as changes nothing outside this script. The module keeps its own name.
Some modules have a short name that almost everyone uses. For example, people load pandas with this line:
import pandas as pdThis short name is a convention, and not a language rule. It is worth following. This is because other people's code uses the same short name.
The third way is one to recognise and then avoid. It is from module import *. The star means every name in the module. So every name is put into your script, with no prefix. This causes two problems. A name from the module can silently replace a name that you already have. And the reader cannot tell which module a name came from.
from os import *
with open("potassium.tsv") as infile:
text = infile.read()
# TypeError: open() missing required argument 'flags' (pos 2)The built-in function open opens files. The module os has a different function with the same name. After the star line, the name open means the function from os. The call then fails. Python gives no warning at the star line. The error message does not mention os. So write import os instead, and keep the prefix. Do not type this example into the Python Scratchpad. This is because the Scratchpad keeps names from one run to the next. So open stays replaced until you reload the page.

4 Installing third-party packages with pip
The standard library does not cover everything. To work with a table of patient data, to draw a chart or to read a file of DNA sequences, you need code that does not come with Python. This code is shared in third-party packages. Other people have written them. You download one when you need it. The tool that downloads a package is called pip.
4.1 Running pip
You run pip in a terminal, and not inside a Python script. The command is pip install, followed by the package name.
For example, the command pip install pandas connects to a public collection called the Python Package Index, or PyPI. The command then downloads the package pandas and puts it on your computer, where Python can find it.
After you install a package, any script can load it with the keyword import. So installing is done once, and loading is done in every script that uses the package.

Suppose a script loads pandas before it has been installed. Python stops at that line and raises a ModuleNotFoundError. The message says which module Python could not find:
import pandas
# ModuleNotFoundError: No module named 'pandas'Read the name in the message first. If the name is spelled correctly, the package is probably not installed yet. So run pip install with that name, and then run the script again.
But if the name in the message is misspelled, installing will not help. This is because Python is looking for a module that does not exist. So correct the spelling in the script instead:
import pandsa
# ModuleNotFoundError: No module named 'pandsa'In a Jupyter or Colab notebook, the same command starts with an exclamation mark, as in !pip install pandas. The ! tells the notebook to run the line as a terminal command, and not as Python.
The Python Scratchpad is not a terminal. The Scratchpad changes a line such as pip install pandas into an install step for you. It also installs a missing package for you. So import pandas works there without an error.
Other packages include numpy for number arrays, matplotlib for charts and scipy for statistical tests.
4.2 Tools and tricks
- To see every installed package with its version, run
pip list. - To remove a package, run
pip uninstallfollowed by its name. - To install one exact version, add two equals signs and the version number after the name, as in
pip install pandas==2.2.3. You do this to reproduce someone else's analysis.
A project that needs several packages lists them in a plain text file called requirements.txt, one package on each line, with its version. One command installs them all:
pip install -r requirements.txt
When you download someone else's project, look for this file first.

Two projects can need different versions of the same package. So each project can have its own separate set of packages. This is called a virtual environment. Two common tools make one: venv, which comes with Python, and conda. You do not need one for this course. But when a tutorial tells you to activate your environment first, this is what it means.

5 Check your understanding
You have reached the end of the module. Try the same questions again — your answers here, paired with your pre-test answers, are how we measure what the module taught you. Answer every question and confidence rating, then click Submit and see results to view your score.
You write import math at the top of your script. Which of these calls works as expected?
After the line from statistics import mean, how do you call the function?
What does the keyword as do in import numpy as np?
Why is from math import * discouraged?
Which of these must you pip install before you can import it?
A script begins with import biopython. You run it and get ModuleNotFoundError: No module named 'biopython'. What is the right next step?
Which command lists every Python package currently installed in your environment?
What is a requirements.txt file used for?
I can import a built-in module like math or statistics and call one of its functions using the module-name prefix.
I can install a third-party package with pip from a terminal and recognise the ModuleNotFoundError that says I need to.
I can read a short script’s import block and tell a peer which modules are built in, which are third-party aliases, and what each module is being used for.
6 Your results
Here is how your post-test answers compare with your pre-test answers. The pre/post pairing is the most reliable way to see what this module actually taught you.
Submit the post-test to see your results.
What is the one thing from this module that is still unclear to you?