Python not in the Notebook

We will often want to save our Python classes, for use in multiple Notebooks. We can do this by writing text files with a .py extension, and then importing them.

Writing Python in Text Files

You can use a text editor like Atom for Mac or Notepad++ for windows to do this. If you create your own Python files ending in .py, then you can import them with import just like external libraries.

You can also maintain your library code in a Notebook, and use %%writefile to create your library.

Libraries are usually structured with multiple files, one for each class.

We group our modules into packages, by putting them together into a folder. You can do this with explorer, or using a shell, or even with Python:

import os

if "mazetool" not in os.listdir(os.getcwd()):
%%writefile mazetool/
from .room import Room
from .person import Person

class Maze:
    def __init__(self, name): = name
        self.rooms = []
        self.occupants = []

    def add_room(self, name, capacity):
        result = Room(name, capacity)
        return result

    def add_exit(self, name, source, target, reverse=None):
        source.add_exit(name, target)
        if reverse:
            target.add_exit(reverse, source)

    def add_occupant(self, name, room):
        self.occupants.append(Person(name, room))
        room.occupancy += 1

    def wander(self):
        "Move all the people in a random direction"
        for occupant in self.occupants:

    def describe(self):
        for occupant in self.occupants:

    def step(self):

    def simulate(self, steps):
        for _ in range(steps):
Overwriting mazetool/
%%writefile mazetool/
from .exit import Exit

class Room:
    def __init__(self, name, capacity): = name
        self.capacity = capacity
        self.occupancy = 0
        self.exits = []

    def has_space(self):
        return self.occupancy < self.capacity

    def available_exits(self):
        return [exit for exit in self.exits if exit.valid()]

    def random_valid_exit(self):
        import random

        if not self.available_exits():
            return None
        return random.choice(self.available_exits())

    def add_exit(self, name, target):
        self.exits.append(Exit(name, target))
Overwriting mazetool/
%%writefile mazetool/

class Person:
    def __init__(self, name, room=None): = name = room

    def use(self, exit): -= 1
        destination =
        destination.occupancy += 1 = destination
        print(, "goes",, "to the",

    def wander(self):
        exit =
        if exit:

    def describe(self):
        print(, "is in the",
Overwriting mazetool/
%%writefile mazetool/

class Exit:
    def __init__(self, name, target): = name = target

    def valid(self):
Overwriting mazetool/

(Required for older versions of Python): In order to tell Python that our “mazetool” folder is a Python package, we have to make a special file called If you import things in there, they are imported as part of the package:

%%writefile mazetool/
from .maze import Maze  # Python 3 relative import
Overwriting mazetool/

Loading Our Package

We just wrote the files, there is no “Maze” class in this notebook yet:

myhouse = Maze("My New House")
NameError                                 Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 myhouse = Maze("My New House")

NameError: name 'Maze' is not defined

But now, we can import Maze, (and the other files will get imported via the chained Import statements, starting from the file.

import mazetool
from mazetool import Maze
house = Maze("My New House")
living = house.add_room("livingroom", 2)

Note the files we have created are on the disk in the folder we made:

import os
os.listdir(os.path.join(os.getcwd(), "mazetool"))
['__pycache__', '', '', '', '', '']

.pyc files are “Compiled” temporary python files that the system generates to speed things up. They’ll be regenerated on the fly when your .py files change.

The Python Path

We want to import these from notebooks elsewhere on our computer: it would be a bad idea to keep all our Python work in one folder.

Supplementary material The best way to do this is to learn how to make our code into a proper module that we can install. We’ll see more on that in a few lectures’ time.

Alternatively, we can add a folder to the “Python Path”, where python searches for modules:

import sys



I’ve thus added a folder to the list of places searched. If you want to do this permanently, you should set the PYTHONPATH Environment Variable, which you can learn about in a shell course, or can read about online for your operating system.