7. Modules

Modules Lab

In this lab, we will learn how to organize our code into modules and import code from other modules.

What is a module?

In the previous lab, we used functions to break up a program into reusable pieces. Modules let you organize those pieces of code so each file doesn’t get too long and so you can reuse them across projects. In this lab we are going to learn how to import code from one module into another. But first, let’s get some vocabulary straight:

  • All python code lives in files that end in .py. They’re just plain text files that you can edit using Atom (or Microsoft Word if you really want. Don’t though. You’ll regret it.)
  • Python thinks of each file as a module. Each module is its own little bubble world.
  • Python thinks of each directory containing .py files as a package. A package is a bundle of modules. So your unit_00 directory is also a package.

Importing

When you want to use something from another module, you need to import it. We have actually already been doing this. Every one of our programs so far has started with from turtle import *. But what is this turtle? Where did it come from?

Navigate to ~/Desktop/making_with_code/pedprog/unit00/lab07, enter a poetry shell, and then enter a Python shell.

💻 Use the Python shell to find out more about the Turtle
$ cd ~/Desktop/making_with_code/pedprog/unit00/lab07
$ poetry shell
$ python
>>> import turtle
>>> turtle
<module 'turtle' from '<some-file-path>/turtle.py'>

See? It’s a module! And it lives in a file called turtle.py somewhere on your computer. If you want, you can copy that file path and open the file in Atom. It’s over 4000 lines and you definitely won’t understand much of it… yet.

>>> turtle.forward
<function forward at 0x102f90d08>

There’s our old friend forward(), the function which makes the turtle walk forward. 0x102f90d08 is a specific memory address where the function is located. Yours will be different.

There are actually three different ways to get forward.

You can import turtle:

import turtle
turtle.forward(100)

Or, you can import forward from turtle:

from turtle import forward
forward(100)

Or, you can do it the way you are used to doing it:

from turtle import *
forward(100)

This third way means “go into the turtle module and import everything!” It’s quick and easy, but it’s kind of sloppy and it’s not always a very good idea.

Imagine this scenario:

from bodily_functions import eat
from refrigerator import *
from trash_can import *

eat(chicken_sandwich)

You can see that the eat function came from bodily_functions, but where did that chicken_sandwich come from? The refrigerator? Or the trash_can? By importing just what we need from other modules, we can make it clear where everything came from, and we can make sure we don’t import stuff we don’t want. (What else did we import from the trash_can? Do we want that in our program?)

✅ CHECKPOINT:

Before you go on, figure out how to open turtle.py on your computer using either the finder or the terminal.

  • In your notebook, describe the steps you took in order to find turtle.py.

Finding packages and modules

There are three places you can import packages and modules from:

  • Some packages, like turtle, come pre-installed with python. The collection of pre-installed packages is called the “standard library.” When you import them, python automatically knows where to find them.
  • Some packages were published online by other software developers. If you install them, you can use them too. Like the built-in packages, python knows where to find these when you import them. Poetry takes care of installing packages you need for each project (though all our programming so far has only needed the standard library).
  • Finally, any packages (directories containing *.py files) or modules (*.py files) that are in the same directory as your Python file can be imported.
👾 💬 Sharing modules
Programming may be the most powerful form of collaboration ever invented. When you write a Python program, you can re-use any of the code in the modules pre-installed in the standard library, or any of the 300k+ modules released by other developers to the Python package index.

Working with new packages

Before you can make use of code from new packages, you have to learn what’s included and how to use it. You could just read the source code but it isn’t always intuitive. Packages usually come with documentation explaining their contents. The package provided in lab 7, superturtle, comes with documentation stored in the documentation directory.

👀 Open the documentation in your web browser by running open documentation/index.html. Read all of the introduction, and then skim the API.

👾 💬 Optional arguments

Some of the functions in superturtle have optional arguments. You can specify values, otherwise a default value will be provided. For example, superturtle.animation.animate can take the following arguments:

superturtle.animation.animate(
    frames=1, 
    loop=False, 
    debug=False, 
    gif_filename=None,
)

None are required, but any can be provided. The behavior of the function changes depending which arguments you provide. For example, if you wanted a looping animation with 60 frames (3 seconds), and saved at funny.gif, you would use animate(frames=60, loop=True, gif_filename="funny.gif").

Create a drawing

💻 Open drawing.py. Create a drawing using at least:

  • 2 functions from the stroke module
  • 2 functions from the movement module

Once you are comfortable creating static drawings, try creating an animated gif.