10. Modules

Modules Lab

In this lab, we will learn how to organize our functions into modules. Modules are files of reusable function. You’ve already encountered modules before!

[0] Setup

๐Ÿ’ป Go int your lab_modules folder in your unit_00 directory:

cd ~/Desktop/making_with_code/cs9/unit00_drawing/lab_modules_YOURGITHUBUSERNAME

Don’t forget to change “YOURGITHUBUSERNAME” to your actual Github username.

๐Ÿ’ป Update the packages
git submodule update --init --recursive
๐Ÿ’ป Enter the Poetry shell:
poetry shell

[1] What is a module?

In the previous lab, we used functions to break up a big program into smaller programs. Modules do the same thing, but on a larger scale!

So let’s say you wrote a function in one file. What if you want to re-use a function you wrote in a different file? Copy it over? It feels like there should be a better way.

Today 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.

[How to import a module]

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? It’s a module! And it lives in a file called turtle.py that some people wrote for you.

There are actually three different ways to get forward.

First, is the way you are used to doing it. This way imports all of the functions in turtle.py. It’s quick and easy, but it’s kind of sloppy and it’s not always a very good idea.

from turtle import *

Or, you can import just the forward function from turtle:

from turtle import forward

Or, you can import the Turtle objects. We will learn more about this in Unit 02: Games. You can import turtle:

import turtle

Imagine this scenario:

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


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?)

[2] Tree

๐Ÿ’ป Let’s start by going into tree_drawing folder.

cd tree_drawing

This folder has 3 files:

  • basic_shapes.py
  • tree_parts.py
  • tree.py
๐Ÿ’ป Open the folder in Atom
atom .

๐Ÿ‘€Take a look at basic_shapes.py and tree_parts.py. Note how tree_parts.py uses functions from basic_shapes.py.

๐Ÿ’ป Currently, tree.py is empty. It’s up to you to write function to draw a full tree. Your tree_full() function should be able to draw a tree of any size.

Be sure to consider:

  • what modules will you need to import?
  • how can you combine exisiting functions to draw a full tree?

When you run tree.py it look like:

[3] Drawing Package

Now we are going to use a package full of fancy drawing functions to turbocharge your turtle.

Packages always include documentation to communicate to users how to use the included software. This lab will require you to read documentation provided in the README.md file.


๐Ÿ‘€ Open the documentation for examples of all the functions available in the drawing package. It has the following modules:

  • shapes.py
  • movement.py
  • lines.py

๐Ÿ’ป cd out of the /tree folder.

cd ..

Here you should have:

  • /lab_modules
    • /drawing package
    • fancy_drawing_example.py - an example usage of the drawing package
    • fancy_drawing.py - an empty file for you to create your own drawing


๐Ÿ’ป Start by running: fancy_drawing_example.py

python fancy_drawing_example.py


  • Why does the drawing just appear?
  • What modules are used from the drawing package?
  • How do the colors repeat?

๐Ÿ’ป Experiment with editing the code. Try make your own version of this pattern! Here is just one of many potential patterns:

๐Ÿ‘€ Explore more color options HERE

[Fancy Drawing]

๐Ÿ’ป In fancy_drawing.py, it is up to you to create a drawing of your choosing using the modules in the drawing package. It must include uses of at least 3 different modules from the drawing package.

atom fancy_drawing.py
Be prepared to share your drawing!

[4] Deliverables


Once you’ve successfully completed the sequence be sure to fill out this Google form.

๐Ÿ’ป Push your work to Github:

  • git status
  • git add fancy_drawing.py
  • git status
  • git commit -m “describe your drawing and your process here”

    be sure to customize this message, do not copy and paste this line

  • git push