Post

Python Import Statement Explained

Python Import Statement Explained

What is the import Statement in Python?

The import statement is Python’s mechanism for incorporating external code into your programs. It allows you to access functions, classes, variables, and other definitions from modules (Python files) that are either built into Python or provided by third-party libraries.

When you use an import statement, Python performs two key operations:

  1. Finds the module - Searches through directories listed in sys.path to locate the module
  2. Initializes the module - Executes the module’s code and makes its definitions available

How Python Finds Modules: The Module Search Path

When you write import module_name, Python searches for the module in this order:

  1. Current directory - The directory containing your script or the current working directory
  2. PYTHONPATH environment variable - A list of directories you specify
  3. Standard library directories - Where Python’s built-in modules are installed
  4. site-packages directory - Where third-party packages are installed

You can view the current search path with:

1
2
import sys
print(sys.path)

Import Statement Syntax and Forms

1. import module_name

Imports an entire module and creates a reference to it in your namespace.

1
2
3
4
5
import math

# Use the module's functions with dot notation
result = math.sqrt(16)  # Returns 4.0
pi_value = math.pi      # Returns 3.141592653589793

2. from module import name

Imports specific names from a module directly into your namespace.

1
2
3
4
5
from math import sqrt, pi

# Use without module prefix
result = sqrt(16)   # Returns 4.0
circle_area = pi * 5**2  # Returns 78.53981633974483

3. import module as alias

Imports a module and gives it an alternative name (alias).

1
2
3
4
5
6
import numpy as np
import pandas as pd

# Use the alias
array = np.array([1, 2, 3])
dataframe = pd.DataFrame({'A': [1, 2, 3]})

4. from module import *

Imports all public names from a module (not recommended for production code).

1
2
3
4
5
from math import *

# All functions available without prefix
result = sqrt(25)   # Returns 5.0
angle = sin(pi/2)   # Returns 1.0

⚠️ Warning: This form can overwrite existing names and makes code harder to read because it’s unclear where each function comes from.

Python Modules vs Packages

Modules

A module is simply a .py file containing Python code. It can define functions, classes, variables, and execute statements.

1
2
3
4
5
6
7
8
9
# mymodule.py
def greet(name):
    return f"Hello, {name}!"

PI = 3.14159

class Calculator:
    def add(self, a, b):
        return a + b

Packages

A package is a directory containing multiple modules and an __init__.py file (optional in Python 3.3+). Packages allow you to organize related modules hierarchically.

1
2
3
4
5
6
mypackage/
├── __init__.py      # Package initialization file
├── module1.py       # First module
└── subpackage/
    ├── __init__.py
    └── module2.py   # Nested module

Practical Examples

Example 1: Creating and Using a Custom Module

First, create a file called calculator.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# calculator.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

Then use it in your main program:

1
2
3
4
5
# main.py
import calculator

print(calculator.add(10, 5))      # 15
print(calculator.multiply(3, 4))  # 12

Example 2: Using Standard Library Modules

Python’s standard library provides modules for almost everything:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Working with dates and times
from datetime import datetime, timedelta

now = datetime.now()
tomorrow = now + timedelta(days=1)
print(f"Today: {now.strftime('%Y-%m-%d')}")
print(f"Tomorrow: {tomorrow.strftime('%Y-%m-%d')}")

# Working with files and paths
import os
from pathlib import Path

current_dir = os.getcwd()
project_root = Path(__file__).parent
print(f"Current directory: {current_dir}")

# Working with JSON data
import json

data = {"name": "Alice", "age": 30, "city": "New York"}
json_string = json.dumps(data)
parsed_data = json.loads(json_string)

Example 3: Using Third-Party Libraries

First install a package (using pip):

1
pip install requests pandas

Then use it in your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests
import pandas as pd

# Fetch data from an API
response = requests.get('https://api.github.com/repos/pandas-dev/pandas')
repo_data = response.json()
print(f"Stars: {repo_data['stargazers_count']}")

# Create and manipulate a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'London', 'Tokyo']
})
print(df.describe())

Web Development

  • Django - High-level web framework for rapid development
  • Flask - Lightweight micro-framework for web applications
  • FastAPI - Modern, fast web framework for building APIs
  • Pyramid - Flexible web framework for applications of any size

Data Science & Analytics

  • Pandas - Data manipulation and analysis with DataFrame structures
  • NumPy - Fundamental package for scientific computing with N-dimensional arrays
  • Matplotlib - Comprehensive 2D plotting library
  • Seaborn - Statistical data visualization built on Matplotlib
  • SciPy - Library for mathematics, science, and engineering

Artificial Intelligence & Machine Learning

  • TensorFlow - Open-source machine learning framework by Google
  • PyTorch - Deep learning framework developed by Facebook’s AI Research lab
  • scikit-learn - Simple and efficient tools for data mining and analysis
  • Keras - High-level neural networks API running on TensorFlow
  • XGBoost - Optimized gradient boosting library

Automation & Scripting

  • Selenium - Web browser automation for testing and scraping
  • PyAutoGUI - GUI automation for controlling mouse and keyboard
  • schedule - Library for scheduling Python functions
  • requests - HTTP library for making requests

DevOps & Infrastructure

  • Ansible - Agentless IT automation tool
  • Boto3 - AWS SDK for Python to manage AWS services
  • Fabric - Library for executing shell commands remotely
  • paramiko - SSH library for Python

Database

  • SQLAlchemy - SQL Toolkit and Object Relational Mapper
  • PyMongo - Python driver for MongoDB
  • Redis-py - Python client for Redis
  • peewee - Small, expressive ORM

GUI Development

  • Tkinter - Standard GUI toolkit included with Python
  • PyQt/PySide - Complete set of Python bindings for Qt framework
  • Kivy - Open-source library for multitouch applications
  • wxPython - Cross-platform GUI toolkit

Testing

  • pytest - Mature full-featured Python testing tool
  • unittest - Built-in unit testing framework
  • hypothesis - Advanced property-based testing library
  • selenium - Web browser automation for testing

Best Practices for Imports

  1. Place imports at the top of your file - This is the conventional location and makes dependencies clear.

  2. Use absolute imports over relative imports - They’re more explicit and easier to understand:
    1
    2
    3
    4
    5
    
    # Good
    from mypackage import module
       
    # Avoid (unless necessary)
    from . import module
    
  3. Group imports logically:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    # Standard library
    import os
    import sys
       
    # Third-party
    import pandas as pd
    import requests
       
    # Local/relative
    from myapp.utils import helper_function
    
  4. Avoid import * in production code - It makes code harder to read and can cause name conflicts.

  5. Use aliases for long module names:
    1
    2
    
    import matplotlib.pyplot as plt
    import numpy as np
    
  6. Handle import errors gracefully:
    1
    2
    3
    4
    
    try:
        import ujson as json
    except ImportError:
        import json
    

The __name__ == "__main__" Pattern

When a Python file is run directly, its __name__ variable is set to "__main__". When imported, it’s set to the module’s name. This allows you to write code that behaves differently when run as a script versus when imported:

1
2
3
4
5
6
7
8
9
10
# mymodule.py
def main():
    print("Running as main program")

if __name__ == "__main__":
    # This code only runs when the file is executed directly
    main()
else:
    # This code runs when the module is imported
    print("Module imported")

0

Reloading Modules

By default, Python only imports a module once per session. To reload a modified module:

1
2
3
4
5
import importlib
import mymodule

# After modifying mymodule.py
importlib.reload(mymodule)

Conclusion

The import statement is fundamental to Python programming. It enables code reuse, modular design, and access to the vast ecosystem of Python libraries. Understanding how imports work helps you write cleaner, more maintainable code and makes it easier to leverage existing tools and frameworks.

Whether you’re using built-in modules like math and datetime, third-party packages from PyPI, or creating your own modules and packages, mastering Python’s import system is essential for effective Python development.

This post is licensed under CC BY 4.0 by the author.