Get AI summaries of any video or article — Sign up free
Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library thumbnail

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

Corey Schafer·
5 min read

Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Importing a module executes its top-level code, so print statements and other side effects inside the module run at import time.

Briefing

Importing Python modules hinges on two practical choices: how to reference code you wrote (or third-party code) and how to pull in built-in tools from the standard library. The core takeaway is that Python can only find a module if it’s located in one of the directories listed in its import search path (sys.path), and once it’s found, importing runs the module’s top-level code—so print statements and other side effects inside a module will execute at import time.

The tutorial starts with a custom module, my module.py, containing a variable (test) and a function find_index that searches a sequence for a target value and returns its index—or -1 if the value isn’t present. A separate script (intro.py) defines courses, then imports my module from the same directory. That “same directory” detail matters because Python’s import mechanism first checks the directory containing the running script. After importing, the function is accessed as my module.find_index(...), and the script passes courses and a target like "Math" to retrieve the index.

To reduce repetitive typing, the tutorial demonstrates three import patterns. First, aliasing: import my module as mm lets code call mm.find_index instead of my module.find_index. Second, importing specific names: from my module import find_index allows direct use of find_index without the module prefix, but it also means other module-level objects (like test) aren’t automatically available. Third, importing multiple specific items: from my module import find_index, test restores access to both. It also shows that renaming imported functions (e.g., import find_index as fi) can shorten code, but quickly harms readability.

A fourth option— from my module import *—pulls everything into the current namespace, including test and find_index. That convenience comes with a downside: it becomes unclear which names came from the module, making debugging harder when something breaks.

Next comes the “where does Python look?” section. Python searches sys.path, which is built from several sources in order: the current script’s directory, directories listed in the PYTHONPATH environment variable, standard library directories, and finally site-packages for third-party libraries. When my module.py is moved to a different folder, importing fails with “ModuleNotFoundError.” The fix is either to append the new folder to sys.path at runtime or, more cleanly, to add that folder to PYTHONPATH.

The tutorial walks through setting PYTHONPATH on both macOS (editing ~/.bash_profile and exporting PYTHONPATH) and Windows (creating a PYTHONPATH environment variable via System Properties). After that, importing my module works again because the new directory now appears in sys.path.

Finally, the standard library is treated as a ready-made toolkit. Examples include random.choice for selecting a random course, math.radians and math.sin for trigonometry, datetime.date.today for today’s date, calendar.isleap for leap-year checks, and os.getcwd for the current working directory. The tutorial also notes that standard library modules are just Python files and can be located via module.__file__. As a playful example, it imports anti-gravity, a joke module that opens a web comic, then encourages learners to browse standard library source code to learn how real modules are built.

Cornell Notes

Python imports custom modules and standard library modules using the same underlying mechanism: Python searches sys.path for module locations, then executes the imported module’s top-level code. Custom modules can be imported as a whole (import my module), aliased (import my module as mm), or selectively (from my module import find_index, test). Selective imports reduce typing but only bring in the names explicitly listed; star imports (from my module import *) make debugging harder because it’s unclear where names come from. If a module isn’t in the script’s directory, adding its folder to sys.path or to the PYTHONPATH environment variable restores imports. The standard library then provides common utilities like random, math, datetime, calendar, and os without extra installation.

Why does importing a module sometimes trigger unexpected output or side effects?

When Python imports a module, it executes all top-level code in that module file, not just function and variable definitions. That’s why the custom module includes a print statement: it confirms that importing runs the module immediately. In practice, any print calls, file operations, or other side effects placed at the module’s top level will run as soon as the import happens.

What’s the difference between import my module, import my module as mm, and from my module import find_index?

import my module loads the module under its module name, so functions are accessed as my module.find_index(...). import my module as mm aliases the module name, so calls become mm.find_index(...). from my module import find_index imports only the function name into the current namespace, so the code can call find_index(...) directly without the module prefix.

Why does from my module import find_index not give access to test?

That import form brings in only the names explicitly listed. If test is defined in my module.py but the import statement only requests find_index, then test isn’t imported into the script’s namespace. To access it, the script must import it too, e.g., from my module import find_index, test.

What does sys.path control, and how does PYTHONPATH fit in?

sys.path is the ordered list of directories Python checks when resolving imports. The tutorial lists the order as: the current script’s directory first, then directories from the PYTHONPATH environment variable, then standard library directories, and finally site-packages for third-party modules. Adding a folder to PYTHONPATH ensures Python can find modules stored outside the script’s directory.

Why is from my module import * considered frowned upon?

Star imports pull every public name from the module into the current namespace, which makes it difficult to tell which names originated from that module. If a bug appears in a function like find_index, it becomes harder to trace where the function was defined or imported from. The tutorial recommends importing specific names to keep debugging clear.

How can learners verify where a standard library module lives on disk?

Standard library modules are Python files, and their location can be inspected via module.__file__. For example, printing os.__file__ reveals the filesystem path to the os module source. Opening that directory lets learners browse the standard library’s code directly, including modules like anti-gravity (a community joke module that opens a web comic).

Review Questions

  1. When a module is imported, which parts of the module execute immediately, and why does that matter for debugging?
  2. Given sys.path’s search order, where would you add a folder so Python can import a custom module stored outside the script directory?
  3. How do selective imports (from my module import ...) change what names are available compared with importing the entire module?

Key Points

  1. 1

    Importing a module executes its top-level code, so print statements and other side effects inside the module run at import time.

  2. 2

    Use import my module to access functions and variables via the module name (e.g., my module.find_index).

  3. 3

    Alias modules with import my module as mm to reduce repetitive typing while keeping the namespace clear.

  4. 4

    Selective imports (from my module import find_index, test) only bring in explicitly listed names, so other module variables won’t be available.

  5. 5

    Avoid from my module import * because it obscures where names came from and complicates debugging.

  6. 6

    Python resolves modules by searching sys.path in order: script directory, PYTHONPATH entries, standard library, then site-packages.

  7. 7

    If a module isn’t found after moving files, add its directory to sys.path or set PYTHONPATH (macOS and Windows require different steps).

Highlights

Python’s import system runs the entire module file at import time, so top-level prints and side effects will execute immediately.
from my module import find_index imports only that function—module variables like test remain unavailable unless explicitly imported.
Star imports make debugging harder because they hide the origin of imported names.
sys.path is built from the script directory, PYTHONPATH, standard library paths, and site-packages, in that order.
Standard library modules like random, math, datetime, calendar, and os provide ready-to-use functionality without extra installation.

Topics

  • Module Imports
  • sys.path
  • PYTHONPATH
  • Standard Library
  • Import Syntax

Mentioned