Skip to content
Tactiverge

Learning Rooms/○ Level 3 · Innovator/Computing and algorithms

Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Split a program into modules, each with one job and a small public interface.
  • Import names from your own modules and explain how Python finds them.
  • Trace which modules must be re-tested when one module changes, and spot a circular import.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

In a planner package, units.py is imported by grid.py and report.py; grid.py is imported by planner.py and io_csv.py; main.py imports io_csv, planner and report. You change units.py. How many other modules could be affected?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The dependency explorer shows seven modules in layers: units.py at the bottom; grid.py and report.py import units; io_csv.py and planner.py import grid; main.py imports io_csv, planner and report; test_planner.py imports planner and grid. Changing units.py affects all five modules above it. Ticking the shortcut makes grid.py also import planner.py, creating a circular import.

3.Watch, actively

Shows how import finds a module, how to import only the names you need, and where Python looks for your own files.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch for the difference between import module and from module import name, and where Python searches for your own files.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

Once a program grows, you split it into modules: files of related functions that other files import. Python looks for a module in a list of places, starting with the script's own folder. Importing only the names you need keeps it clear where each one came from.

What the video covers (our own note, written when we verified it): Shows how import finds a module, how to import only the names you need, and where Python looks for your own files.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

In a package with no circular imports, which module's changes need the MOST re-testing elsewhere?

Practice 2

grid.py imports planner.py so GridMap can call bfs, and planner.py imports grid.py. What is the best fix?

Practice 3

planner.py contains def _neighbours_in_bounds(...). What does the leading underscore tell other programmers?

Practice 4

io_csv.py ends with print(load_map('mars.csv')) at the top level. What happens when planner tests run 'import io_csv'?

5.Try it yourself

No simulation models this idea yet. The lesson below has an interactive diagram instead.

6.Where engineers use it

The Robot Operating System (ROS) splits a robot into packages and nodes with declared dependencies, so a team can rebuild and test only the parts affected by a change. Your four-file planner is the same idea at a smaller scale.

From the applied view in the lesson One script is not a system. Checked by automated tests only.

7.Build something with it

Follow-up project

Take a script you wrote earlier (or the starter planner in the Code Lab) and split it into at least three modules: one for unit conversions, one for loading data and one for the calculation. Draw the dependency arrows on paper. Check there are no loops, and list which modules you would re-test if each one changed.

Where this video lives

In the course catalogue: CSE-310 Structured programs and data

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.