Confused by Python imports? In this beginner-friendly tutorial, we break down exactly how to navigate large Python libraries and cleanly import specific sub-packages.
Key Takeaways 📖
📦 Package vs. Library
Package: A physical folder on your computer containing code files and marked by an initialization file.
Library: A conceptual collection of reusable code that can contain one or multiple packages.
🗺️ 3 Ways to Import Sub-Packages
Using the example of accessing stats inside scipy:
🔹 Direct Import (import scipy.stats): Navigates via dot notation (.). Requires you to type the full path every time: scipy.stats.normaltest().
🔹 Aliasing (import scipy.stats as stats): Creates a short nickname (stats), letting you skip the top-level scipy package folder: stats.normaltest().
🔹 from . . . import (from scipy import stats): Pulls the stats sub-package folder directly into your notebook: stats.normaltest().
⚠️ Critical Watchouts
🛑 The Wildcard Character: Avoid blindly dumping hundreds of tools into your environment using *.
⚡ Overwriting Code: If two different libraries have sub-packages with the same name (e.g., stats), importing the second one will overwrite your original shortcut and break your code.
🚀 Master the foundations: Enroll in the full "First Steps in Python: Install, Import & Manage Libraries" course and earn your Foundational Certificate:
https://traindocai.com/courses/
Video Chapters:
0:00 Introduction
0:18 Importing a Specific Sub-Package
0:54 Drilling Deeper: Navigating with the Dot (.)
1:06 Importing Specific Sub-Packages: from...import
1:29 How to call a tool
2:04 Wildcard Character
2:36 Package vs. Library
3:05 Reading the Tree