Modules are fundamental to the programming model of Python. Modules are programming units that (should) consist of related variables and functions that form a coherent block of data+procedures (functions). They are an extension of the concept of packaging code that we have already studied with functions.

In Python every file you write is a module. The name of the module is the file name without the extension. Every Python file must end in .py regardless of your operating system, so if you write a file the corresponding module is mycode. If you are using Jupyter you will need to export your code into a script in order to create a new module (Notebook tab -> Export to -> Executable Script).

Modules that are not run as the main program must be imported for its contents to be accessible. When a module is imported, it is compiled to a special representation called bytecode. A new file of the same base name with the suffix .pyc will be created.

Many modules and packages (collections of modules) are available through a base Python installation. Anaconda provides dozens more, with others available for installation through the Environments tab of the Navigator. We have already seen a handful of these built-in modules.

import math 
import matplotlib.pyplot

When we import a module we bring in its namespace. A namespace is an environment that holds a group of identifiers, such as variable and function names. In Python the namespaces take the name of the module in which they are defined. Namespaces can be renamed when the module is imported, but not afterward.

Importing Modules

With a simple import statement we must refer to the module’s components with its native namespace.

import math 
import os 
import numpy 


We can select only certain components with from

from modulename import func1, func2

Now only func1 and func2 can be used, and we do _not* precede their names with the native namespace.


To import all symbols without a prefix use

from modulename import *

This statement imports all names that do not begin with a single underscore (_) and makes them accessible without being preceded by the module name.

We can also rename individual symbols

from math import sqrt as squareroot

One of the most commonly used versions of import changes the name of the namespace, typically to something simpler.

import numpy as np
import pandas as pd


Main Modules

When you run a script directly through the interpreter, such as by using the Run arrow in Spyder, it is in the “main” namespace. Your module can also be imported into the interpreter or into another module. It will still execute everything in the module, including requests for input and the like, unless you use the special variables __name__ and __main__ (two underscores on each side). If you use __main__ you can place all code you want to execute only when run directly after a conditional.

if __name__=="__main__":

It is customary to include code for the main namespace into a function named main().

def main():

if __name__=="__main__:"


#from __future__ import print_function, division #Python 2.7
from math import sqrt

def MySqrt(x):
    """Babylonian method."""
    while abs(my_sqrt-s0)>tol:
    return my_sqrt

def relerr(x1,x2):   
    return abs((x2-x1)/x2)

def main():
    for i in range(-N,N+1):

if __name__=="__main__":


Type in the example. Save it into a file called Type in and save a file

import rooter

First run as a standalone script, then run What’s the difference?