Modules and Packages
Importing Standard Modules in Python
Python comes with a rich collection of standard modules that can be imported directly, without installation. These provide useful functionalities for math, data, files, system operations, and more.
Simple Import
import math
import datetime
Import with Alias
import math as m
print(m.sqrt(16)) # Displays: 4.0
Partial Import
from datetime import date
print(date.today()) # Displays: current date
- Standard modules are part of the official Python distribution.
- Aliases (
as) help shorten the code. - Selective import (
from ... import ...) reduces the namespace.
Creating Your Own Modules in Python
A module is a Python file containing functions, classes, or variables that can be reused in other files. Creating your own modules helps organize code and separate logic into clear components.
Creating a Module
# File: salut.py
def salut(name):
return f"Hello, {name}!"
Importing the Module
# Main file: main.py
import salut
print(salut.salut("John")) # Displays: Hello, John!
Selective Import
from salut import salut
print(salut("Anna")) # Displays: Hello, Anna!
- A module is any
.pyfile that can be imported. - The module name must be valid and contain no spaces.
- You can split your application into modules for a clearer and testable structure.
Python Standard Library: math - random - datetime - os
The Python standard library provides powerful modules for mathematical operations, random generation, date/time manipulation, and file system interaction.
math - Mathematical Functions
import math
print(math.sqrt(25)) # Displays: 5.0
print(math.pi) # Displays: 3.141592653589793
random - Random Generation
import random
print(random.randint(1, 10)) # Integer between 1 and 10
print(random.choice(["red", "green", "blue"])) # Random element from list
datetime - Working with Dates and Times
from datetime import datetime
now = datetime.now()
print(now.strftime("%d-%m-%Y %H:%M")) # Displays current date and time
os - File System Interaction
import os
print(os.getcwd()) # Displays current directory
print(os.listdir()) # Lists files in directory
- Modules from the standard library do not require separate installation.
mathandrandomare useful in algorithms and simulations.datetimeis essential for working with time and scheduling.osenables automation of file and directory tasks.
Installing Packages with pip
pip is the official package manager for Python. It allows you to install external libraries from
the Python Package Index (PyPI) and integrate them into your projects.
Installing a Package
pip install requests
Checking Installed Packages
pip list
Updating a Package
pip install --upgrade requests
Uninstalling a Package
pip uninstall requests
- Python must be correctly installed on your system to use
pip. - Packages installed with
pipcan be used immediately viaimport. - You can install specific versions:
pip install name==version.
Structure of a Python Package
A Python package is a directory containing one or more modules and a special file __init__.py.
Packages allow organizing code logically and reusing components.
Basic Structure
project/
├── util/
│ ├── __init__.py
│ ├── calc.py
│ └── text.py
├── main.py
Importing from a Package
# File: main.py
from util.calc import add
from util.text import uppercase
The __init__.py File
# File: util/__init__.py
# Can be empty or expose common functions
- The
__init__.pyfile marks the directory as a Python package. - You can create nested packages for multi-level organization.
- Imports must respect the folder structure.
Using venv for Virtual Environments in Python
venv is Python's standard module for creating virtual environments. They allow isolation of
packages and dependencies per project, avoiding version conflicts.
Creating a Virtual Environment
python -m venv env
Activating the Environment
# Windows
env\Scripts\activate
# macOS / Linux
source env/bin/activate
Deactivating the Environment
deactivate
Installing Packages in the Environment
pip install flask
- Each virtual environment has its own
pipand installed packages. - Activating the environment changes the terminal prompt.
- Using
venvis recommended for any serious project.
