Importing modules
In this lesson, you’ll learn how to import modules in Python, including importing entire modules, importing specific items, and using aliases. By the end, you’ll be able to organize code more effectively across projects. If you’re new to Python, you may want to start with Introduction to Python to get up to speed.
What Is a Module?
A module is a file containing Python definitions and statements, such as functions, variables, and classes. Modules allow you to logically organize your Python code by grouping related functions, classes, and variables into separate files. This way, you can reuse code across multiple programs.
How to Import a Module
You can import a module in Python using the import statement. There are several ways to import modules, and each method allows you to control the namespace and access of the module’s contents.
1. Basic Import
The simplest form is to import the entire module. This gives you access to all the functions, classes, and variables defined in the module, and you can reference them using the module name.
Example:
import math
print(math.sqrt(16)) # 4.0
- math is a built-in module that provides mathematical functions.
- You access its sqrt() function by prefixing it with the module name (math).
2. Import Specific Functions or Variables
You can import specific functions, classes, or variables from a module, which allows you to avoid using the module name as a prefix.
Example:
from math import sqrt
print(sqrt(16)) # 4.0
- Here, we import only the sqrt() function from the math module, so you don’t need to prefix it with math.
3. Importing with Aliases
You can import a module or function with an alias (a shorter name) using the as keyword. This is helpful when the module name is long or when you want to avoid naming conflicts.
Example:
import numpy as np
array = np.array([1, 2, 3])
print(array) # [1 2 3]
- Here, numpy is imported as np to save typing and make the code more concise.
4. Import All (Not Recommended)
You can import everything from a module using the * syntax. However, this is generally discouraged because it can lead to name conflicts and make the code harder to understand.
Example:
from math import *
print(sqrt(16)) # 4.0
- This imports everything from the math module, so you don’t need to use math. as a prefix.
- Caution: This can overwrite any existing functions or variables in the namespace with the same name.
5. Importing from a Submodule
- Sometimes, a module contains submodules or subpackages. You can import a submodule or a specific item from it.
Example:
from datetime import datetime
now = datetime.now()
print(now)
Here, datetime is a submodule of the datetime module. We import it directly.
Summary of Importing Modules
| Syntax | Description | Example |
|---|---|---|
import module_name |
Imports the entire module (access items with module_name.item) |
import math |
from module_name import item |
Imports specific items directly into current namespace | from math import sqrt |
import module_name as alias |
Imports module with a shorter alias name | import numpy as np |
from module_name import * |
Imports all items from module (can cause namespace pollution) | from math import * |
from module_name.submodule import item |
Imports specific item from a submodule | from datetime import datetime |
Using external modules is common in real-world projects. To install and manage external libraries, you can use pip, and for more on integrating external libraries into your Python projects, see the Working with External Libraries course.
Using External Modules
In addition to the built-in modules like math, Python allows you to use third-party libraries. To use an external module, you often need to install it first, which can be done using pip (Python’s package installer). For practical guidance on working with external libraries, consider the Working with External Libraries course.
For example:
pip install requests
Once installed, you can import it just like any other module:
import requests
response = requests.get(“https://www.example.com”)
print(response.status_code)
Standard Library overview
The Python Standard Library is a collection of modules and packages that come bundled with Python. It provides a wide range of functionality, from basic data structures to advanced networking, file handling, and internet protocols. The Standard Library is one of Python’s most powerful features because it provides well-tested, robust tools for many common tasks, saving you from reinventing the wheel. If you’re new, you might start with Introduction to Python for a quick tour.
Here’s an overview of the Python Standard Library:
Key Categories of the Python Standard Library
1. Text Processing
- string: Contains common string operations and constants.
- re: Provides support for regular expressions.
- textwrap: Useful for formatting text for output (e.g., wrapping long lines).
- unicodedata: Provides access to the Unicode Character Database.
Example (Regular Expressions):
import re
pattern = r”\d+”
text = “There are 123 apples”
result = re.findall(pattern, text)
print(result) # [‘123’]
2. Data Serialization
- json: Used for parsing and producing JSON (JavaScript Object Notation) data.
- csv: Helps with reading and writing CSV files.
- pickle: Allows Python objects to be serialized into a byte stream and vice versa.
Example (JSON):
import json
data = {“name”: “Alice”, “age”: 30}
json_string = json.dumps(data)
print(json_string) # ‘{“name”: “Alice”, “age”: 30}’
3. File and Directory Access
- os: Provides functions for interacting with the operating system, such as manipulating files and directories.
- shutil: Offers a higher-level interface for file operations, including copying and removing files.
- pathlib: A modern approach to handling file system paths.
Example (File Operations):
import os
os.mkdir(“new_folder”) # Creates a new folder
4. Mathematics and Numbers
- math: Contains mathematical functions, constants, and operations like trigonometry, logarithms, and square roots.
- decimal: Provides support for fast floating-point arithmetic with arbitrary precision.
- random: Useful for generating random numbers and performing random selections.
Example (Random Number):
import random
print(random.randint(1, 10)) # Random number between 1 and 10
5. Internet and Networking
- socket: Implements low-level networking interfaces, including client-server communication.
- http: Modules for working with HTTP, including http.client and http.server.
- email: Provides email handling functionalities like parsing and constructing email messages.
Example (Socket):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((‘www.example.com’, 80))
6. Concurrency and Parallelism
- threading: Provides a way to create and manage threads for concurrent execution.
- asyncio: Enables asynchronous programming for managing I/O-bound tasks concurrently.
- multiprocessing: Allows parallel processing using multiple processes.
Example (Threading):
import threading
def print_numbers():
for i in range(5):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
7. System and OS Utilities
- sys: Provides access to system-specific parameters and functions, such as reading command-line arguments.
- platform: Used to access the underlying platform (OS) information.
- time: Provides time-related functions, like measuring elapsed time and sleeping.
Example (System Info):
import sys
print(sys.version) # Prints Python version
8. Functional Programming Tools
- functools: Contains higher-order functions and tools for functional programming, such as reduce(), partial(), and memoization.
- itertools: Provides functions that work on iterators to produce complex iterators, such as combinations, permutations, and infinite sequences.
Example (Partial Function):
from functools import partial
def multiply(x, y):
return x * y
double = partial(multiply, 2)
print(double(5)) # 10
9. Debugging and Testing
- pdb: Python debugger for interactive debugging.
- unittest: Unit testing framework for writing and running tests.
- traceback: Provides utilities for extracting and formatting stack traces.
Example (Unittest):
import unittest
class TestMath(unittest.TestCase):
def test_addition(self):
self.assertEqual(2 + 2, 4)
unittest.main()
10. XML and Data Formats
- xml.etree.ElementTree: Implements simple methods for parsing and creating XML data.
- csv: For reading and writing CSV files.
- configparser: Used for handling configuration files in .ini format.
Example (XML Parsing):
import xml.etree.ElementTree as ET
tree = ET.parse(‘data.xml’)
root = tree.getroot()
print(root.tag) # Prints the root element’s tag
How to Access the Python Standard Library
Since the Standard Library is built into Python, you don’t need to install anything additional to use it. You can simply import any module or package from it.
For example:
import os # OS interface
import math # Mathematical functions
import random # Random number generation
The modules in the Python Standard Library are extremely powerful and will cover most of your development needs, whether you are working on data processing, web development, automation, or testing.
Summary:
The Python Standard Library is vast and provides modules for:
- Text processing (e.g., string, re)
- Data serialization (e.g., json, csv)
- File and directory management (e.g., os, shutil)
- Mathematics (e.g., math, random)
- Internet and networking (e.g., socket, http)
- Concurrency (e.g., threading, asyncio)
- System utilities (e.g., sys, platform)
- Functional programming (e.g., functools, itertools)
- Debugging and testing (e.g., unittest, pdb)
Creating and using your own modules
Creating and using your own modules in Python allows you to organize and reuse code efficiently. It’s one of the core principles of modular programming, where large programs are divided into smaller, manageable pieces. Let’s go through the process step-by-step.




