This chapter is crucial for writing professional, scalable code by teaching you how to organize code into separate files and manage external dependencies. We’ll cover Python’s Standard Library, the import statement, and the essential practice of using Virtual Environments.
1. Modules and the import Statement
A Module is simply a single Python file (.py) containing reusable definitions, functions, and variables. Using modules helps organize code, making it readable and preventing naming conflicts.
A. Basic Importing
| Syntax | Description | Example |
import module_name | Imports the entire module. You must reference its contents using the module name. | import math then math.sqrt(4) |
from module import item | Imports only the specified item (function, variable, or class) directly into the current namespace. | from math import sqrt then sqrt(4) |
import module as alias | Imports the module but gives it a shorter, local alias. | import numpy as np |
# 1. Importing an entire module from the Standard Library
import random
print(random.randint(1, 10))
# 2. Importing a specific function
from datetime import date
today = date.today()
print(today)
B. The Standard Library
Python comes with a huge collection of pre-installed modules called the Standard Library. You should always check the documentation before writing code for a common task, as a solution often already exists.
- Examples:
math,random,datetime,os(operating system interaction).
2. Packages
A Package is a way of structuring Python’s module namespace using “dotted module names.” It is essentially a directory containing multiple modules and a special file named __init__.py (though this file is optional in Python 3.3+).
- Structure:
my_app/
├── __init__.py
├── main.py
├── utils/
│ ├── __init__.py
│ └── helper.py
└── data/
└── processor.py
- Usage: To use the
helper.pymodule insidemain.py, you would use:
from utils import helper
# OR:
from utils.helper import some_function
3. Virtual Environments (Crucial for Professional Code)
When you install external packages (libraries not in the Standard Library), they are typically placed in your system’s global Python installation. This creates conflicts when different projects require different versions of the same library.
A Virtual Environment solves this by creating an isolated directory for each project.
- Isolation: Every project gets its own independent set of Python packages and dependencies.
- Tool: The built-in
venvmodule is the standard tool.
A. Creating and Activating venv
These commands are run in your project’s terminal directory.
- Create the Environment:
python -m venv myenv - Activate the Environment:
- Windows (Command Prompt):
myenv\Scripts\activate
- macOS/Linux (Bash/Zsh):
source myenv/bin/activate
(myenv)). - Windows (Command Prompt):
B. Installing and Managing Dependencies
With the environment active, you install packages using the pip package installer.
- Install a Package (e.g., Requests):
pip install requests
- Create a Requirements File: Lists all packages needed for the project. Essential for sharing code.
pip freeze > requirements.txt
- Install from Requirements File (for a new developer):
pip install -r requirements.txt
- Deactivate:
deactivate
