Python Importing Modules

Importing Your Own Module

A module is a Python program which can be called into another Python program.  All Python files saved as .py can be used as a module.  This can be a very powerful tool.  Many people who write useful python programs they know will be needed again, save their work at places like  GitHub to be used as modules.

Let’s look at a rather useless example to see what is going on with module importing.  Create 2 Python files named “moduleKnockKnock” and “moduleMakeJoke.”  Copy the below code into your files.

#moduleKnockKnock

def knockKnock():
    print("Knock, knock!")

def whosThere():
    print("Who is there?")

def babyOwl():
    #print("Baby owl.")
    return "Baby owl"

def who():
    print(babyOwl(), "who?")

def punchLine():
    print("Baby owl see you later!")
#module makeJoke

import moduleKnockKnock

moduleKnockKnock.knockKnock()
moduleKnockKnock.whosThere()
print(moduleKnockKnock.babyOwl())
moduleKnockKnock.who()
moduleKnockKnock.punchLine()

Can you see what is is happening?  moduleMakeJoke is importing moduleKnockKnock which give it access the the functions in moduleKnockKnock.  Then we call the functions we wish to use from the imported module.

We changed up the style in function babyOwl by adding a print and a return.  The principle is the same.  This is just to expose you to other ways and to keep it from being too boring.

 

Standard Library

We can also import modules from the Python Standard Library.  There are many to chose from but two common ones are “math” and “random.”   You may ask yourself, “Why not just have math and random always included if they are common?”  Great question, the Python program developers decides which to include and exclude.  For every module you include things run just a little slower.   You may not notice speed changes on your computer running small programs, but some programs are very large.  There is also the idea that a program may be run on a slow computer or slow network.

If you want to do more advanced math functions then you will need to import math.

import math   # This will import math module

print("math.sqrt(100) : ", math.sqrt(100))
print("math.sqrt(49) : ", math.sqrt(7))
print("math.sqrt(math.pi) : ", math.sqrt(math.pi))

Now take a look at importing a random number generator

import random

for i in range(2):

    # random float: 0.0 <= number < 1.0
    print(random.random())
   

    # random float: 10 <= number < 20
    print(random.uniform(10, 20))
    

    # random integer: 1 <= number <= 100
    print(random.randint(1, 100))


    # random integer: even numbers in 100 <= number < 500 (start, stop, skip)
    print(random.randrange(100, 500, 2))
    print()

The importable module from the Standard Library are explained at Python.org.

 

Note:  Here is a cool trick!  Open IDLE, type “import math” enter, then type “dir(math)” enter.  You will see a list of all the math functions available.

Congrats!  You are doing well if you go this far!