Python Tutorial: Modules & Packages
Split code across files. Import from the standard library. Install third-party packages with pip.
Organize your code
A single Python file can grow enormous. Modules let you split code across files. Packages bundle related modules. The standard library gives you hundreds of useful tools.
Ten steps.
What you'll learn in this Python modules & packages tutorial
This interactive Python tutorial has 8 hands-on exercises. Estimated time: 18 minutes.
- The standard library — free tools, no install — Python ships with hundreds of built-in modules — the standard library. Math, file system, dates, random numbers, network…
- from import — no prefix needed — `from module import name` imports just what you need — no module prefix required:
- import as — shorter names — `import module as alias` gives the module a shorter nickname:
- os — your file system, your rules — The `os` module gives you access to the operating system — file paths, environment variables, directory listing, file ex…
- sys — talking to the interpreter — `sys` gives you information about Python itself:
- collections — data structure superpowers — `collections` extends Python's built-in containers:
- __name__ == "__main__" — the essential guard — When Python runs a file directly, `__name__` is `"__main__"`.
- Build a file utility — Final challenge — no starter code.
Python Modules & Packages concepts covered
- Organize your code