Munch Lab

Python exercise: Files and modules

This page contains both some explanation of the topics we cover and some exercises for you
to do. Read through the page and complete the exercises at the end.

Scripts and modules

In Python programming you will often hear of both “scripts” and “modules”. They are
really the same thing — just a file with Python code in it.

The only difference is really in intent.

A “script” is a file with Python code that is intended to be run as a program, and it will
typically contain a number of program statements and some print commands to show the
output of the calculations.

A “module”, on the other hand, is a file with Python code that is intended as building
blocks that other code can build on. It will typically contain a number of functions (or
in more complex code “classes” which we won’t cover in this course).

Modules are typically not intended to be run directly. Instead their functionality is
intended to be “imported” into scripts or other modules so their code can be reused
there. This way, other modules do not have to contain copies of the code, they can use the
code in other files.

Importing modules

To import a module you use the “import” command. For example

import math

will import the math module that contains a number of mathematical functions.

After you have imported a module like math, you can see the functions that it contains by calling


and you can get help text for the module by typing


To call a function from the module, say the square root function sqrt(), you cannot call
sqrt() directly but you have to prefix it with the module name, a dot, and then the name,
as in


This is to prevent classes between functions in different modules with the same name.

It is possible to just import a function and then avoid needing the prefix. A slightly
different command is used for this

from math import sqrt

Now sqrt() is imported and can be used without the prefix (but only sqrt() is imported that way).

To import everything from the math module so you don’t need the prefix, you can use

from math import *

but I will not recommend importing this way too often because of the name clash problem
that having the prefixes alleviates.

Writing your own modules

Since a module is just a file with Python code in it, modules are very easy to create. If
you write a number of functions in the editor from IDLE, all you have to do to create a
module is to save the file.

If you save the module with the name, for instance, you have created a module
called module. The suffix, “.py” tells Python that this is a Python file, and it will
consider it a module if you tell it to load a module of the same name as a file (except
for the .py suffix), so to import your new module, you just type

import module

Because Python doesn’t know where your files are located by default, your module file has
to be in one of the directories where Python searches for modules. The easiest way to
ensure this is to put your module files in the same directory as the file that imports

Using IDLE you will not be able to import a module in the shell or in a new files you have
not saved yet, only in files that you have saved in the same directory as the module.

Combining modules and scripts

If you have a file that you want to use both as a module and as a script, but where you
don’t want the script part to be run when the module is imported — say if an import is
only supposed to give you the functions in the module but the script is supposed to call
lots of them and output the result — you can use this construction:

if __name__ == '__main__':
    # here you can do all the script specific stuff

The variable __name__ will contain the name of the current module — which is the name of
the file you are using when the file is being imported as a module. If the file is being
run as a script then __name__ will be set to '__main__'.


Open a new file in IDLE (“New Window” in the “File” menu) and save it as in
the directory where you keep the files you create for this course. Then copy the functions
you wrote for calculating volumes and areas in the “Control Flow and Functions” exercise
into this file and save it.

Now open a new file and save it in the same directory. You should now be able to import
your own module like this:

import geometry

Try and add print dir(geometry) to the file and run it.

Now write a function pointyShapeVolume(x, y, squareBase) that calculates the volume of a square
pyramid if squareBase is True and of a right circular cone if squareBase is False. x is the length of an
edge on a square if squareBase is True and the radius of a circle when squareBase is False. y is the height
of the object. First use squareBase to distinguish the cases. Use the circleArea and squareArea
from the geometry module to calculate the base areas. Example usage:

pyramidVolume = pointyShapeVolume(4, 2.6, True)

coneVolume = pointyShapeVolume(4, 2.6, False)

Solution to exercise

Download linked file

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: