Skip to content

Modules

Modules are files containing Python code. They can define functions, classes, and variables. Python modules are imported using the import keyword.

Python
1
2
3
4
5
6
7
8
# Import the math module
import math

# Use the math module
print(math.sqrt(16))

# Output:
# 4.0

Importing Specific Items

You can import specific items from a module using the from keyword.

Python
1
2
3
4
5
6
7
8
# Import the sqrt function from the math module
from math import sqrt

# Use the sqrt function
print(sqrt(16))

# Output:
# 4.0

Aliasing Modules

You can alias a module using the as keyword.

Python
# Import pandas module and alias it as pd
import pandas as pd # this is a common data science convention

# Use the pd alias
df = pd.DataFrame()

# Output:
# Empty DataFrame
# Columns: []
# Index: []

Standard Library

Python comes with a standard library that provides a wide range of modules for performing various tasks. You can find the list of standard library modules here.

Third-Party Libraries

In addition to the standard library, Python has a vast ecosystem of third-party libraries that can be installed using package managers like pip. Some popular third-party libraries include:

  • NumPy: For numerical computing.
  • pandas: For data manipulation and analysis.
  • Matplotlib: For data visualization.
  • TensorFlow: For machine learning.
  • Django: For web development.

You can find a list of Python packages here.

Creating Your Own Modules

Modules help break down large programs into smaller, more manageable pieces. You can create your own modules by defining functions, classes, and variables in a Python file.

For example, let's say you have the following directory structure:

Text Only
1
2
3
myproject/
    main.py
    mymodule.py
mymodule.py
1
2
3
4
# Create a module named mymodule.py
# mymodule.py
def greet(name):
    print(f"Hello, {name}!")
main.py
1
2
3
4
5
6
7
8
# Import the greet function from mymodule
from mymodule import greet

# Use the greet function
greet("Alice")

# Output:
# Hello, Alice!

When you import your own modules from another file, the module has to be in the same directory as the file you are importing it from.

Setting Up a Directory as a Module

The __init__.py file is used to mark a directory as a Python package. It can be empty or contain initialization code if necessary. For example, let's say you have a directory named mypackage with the following structure:

Text Only
1
2
3
4
5
6
myproject/
    main.py
    mypackage/
        __init__.py
        module1.py
        module2.py

To import the mypackage directory as a module, you can use the following syntax:

__init__.py
import mypackage.module1
import mypackage.module2

Then, you can import the mypackage module in your main.py file as follows:

main.py
1
2
3
4
5
import mypackage

# Use the module1 and module2 modules
mypackage.module1.foo()
mypackage.module2.bar()

Summary

Modules allow you to import different pieces of code and structure large projects.