Skip to content

Modules and Packages

Modules

In Python, a module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. For example, a module named example would be in a file named example.py.

You can use the import statement to import a module into your code. Once a module is imported, you can use its functions, classes, and variables in your program.

# Syntax
import module_name
# Example
import math
print(math.pi)  # Output: 3.141592653589793

In the example above, we import the math module and use its pi variable to print the value of π.

You can also import specific functions, classes, or variables from a module using the from keyword.

# Syntax
from module_name import name1, name2, ...
# Example
from math import pi, sqrt
print(pi)   # Output: 3.141592653589793
print(sqrt(16))  # Output: 4.0

In the example above, we import the pi and sqrt functions from the math module and use them in our code.

Using Built-in and Third-Party Modules

Python comes with a wide range of built-in modules that provide useful functions and classes for various tasks. Some of the most commonly used built-in modules include math, random, datetime, os, sys, and json.

In addition to built-in modules, you can also use third-party modules created by other developers. These modules can be installed using the Python Package Index (PyPI) and the pip package manager.

To install a third-party module, you can use the following command in your terminal or command prompt:

pip install module_name

Once installed, you can import and use the module in your code just like any other module.

Creating Your Own Modules and Packages

You can create your own modules by writing Python code in a file and importing it into other programs. To create a module, simply save your code in a file with a .py extension and import it using the import statement.

For example, if you have a file named my_module.py containing the following code:

# my_module.py
def greet(name):
    print("Hello, " + name)

You can import the greet function from the my_module module and use it in your program.

# main.py
import my_module

my_module.greet("Alice")  # Output: Hello, Alice

You can also organize related modules into packages by creating a directory with an __init__.py file. This file can be empty, but it signals to Python that the directory should be treated as a package.

For example, if you have a directory structure like this:

my_package/
    __init__.py
    module1.py
    module2.py

You can import the modules from the package using dot notation.

# main.py
import my_package.module1
import my_package.module2

In the example above, we import the module1 and module2 modules from the my_package package and use them in our program.