Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library
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.
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?
What’s the difference between import my module, import my module as mm, and from my module import find_index?
Why does from my module import find_index not give access to test?
What does sys.path control, and how does PYTHONPATH fit in?
Why is from my module import * considered frowned upon?
How can learners verify where a standard library module lives on disk?
Review Questions
- When a module is imported, which parts of the module execute immediately, and why does that matter for debugging?
- Given sys.path’s search order, where would you add a folder so Python can import a custom module stored outside the script directory?
- How do selective imports (from my module import ...) change what names are available compared with importing the entire module?
Key Points
- 1
Importing a module executes its top-level code, so print statements and other side effects inside the module run at import time.
- 2
Use import my module to access functions and variables via the module name (e.g., my module.find_index).
- 3
Alias modules with import my module as mm to reduce repetitive typing while keeping the namespace clear.
- 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
Avoid from my module import * because it obscures where names came from and complicates debugging.
- 6
Python resolves modules by searching sys.path in order: script directory, PYTHONPATH entries, standard library, then site-packages.
- 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).