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 like you have just learned how to make.

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 blogs 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

dir(math)

and you can get help text for the module by typing

help(math)

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

math.sqrt(4)

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 either EPDLab or IDLE, all you have to do to create a module is to save the file.

Presto! You have a new module.

If you save the module with the name module.py, 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 module files in the same directory as the file that imports them.

For the tools we use, it means that we won't necessarily be able to import a module in the shell or in all of the new files we make, only in files we save in the same directory.

Try writing a module with the functions you wrote last week for finding areas and volumes.

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__'.

Index

Contact info

Office address:
Bioinformatics Research Centre (BiRC)
Aarhus University
C.F. Møllers Allé 8
DK-8000 Aarhus C
Denmark
Office phone:
+45 871 55558
Mobile phone:
3013 8342
Email:
kaspermunch@birc.au.dk