Skip to content

Importing Specific Functions Classes Modules Python

Updated: at 03:34 AM

Python modules are files containing Python code, including definitions for functions, classes, and variables. When you import a module, all the code in that module is executed and the definitions are loaded into memory. This allows you to reuse code across multiple files and access definitions easily.

However, sometimes you may only need certain functions or classes from a module rather than everything. Importing specific elements has advantages like cleaner namespaces, better readability, and faster load times. Luckily, Python provides easy ways to import only what you need.

Key concepts are accompanied by annotated code examples for clarity. You will gain a deep understanding of how to properly leverage Python’s flexible import system to create clean, modular code. Let’s get started!

Table of Contents

Open Table of Contents

Python Module and Import Basics

Before diving into selective imports, let’s recap some Python import fundamentals:

What is a Module?

A Python module is simply a Python script file containing function, class, variable definitions and other executable code. By convention, module filenames end in .py.

Modules allow code reuse by letting you import definitions from one file into another. The import statement is used to load a module.

# Import the math module
import math


Python searches a list of directories called the PYTHONPATH to locate modules. This includes the current directory and standard library directories. You can view/modify the PYTHONPATH through sys.path.

Importing Modules

The basic import statement loads the target module and all its definitions. An alias can optionally be used to reference the module with a different name.

# Import module 'math'
import math

# Import module as 'm'
import math as m

From Imports

The from form allows importing specific definitions from a module directly into the current namespace.

# Import the sqrt function from math
from math import sqrt

# Reference sqrt directly

This recap covers the key concepts needed to understand selective imports. Now let’s explore the techniques for importing only what you need.

Techniques for Selective Imports in Python

Python provides several ways to import specific functions, classes, variables, or submodules rather than everything:

1. Basic Import

Import the module normally, then access the desired definitions with dot notation.

# Import the full math module
import math

# Access pi constant directly from math

2. From Import

Use from <module> import to import names directly into current namespace.

# Import sqrt function from math module
from math import sqrt

# Use sqrt directly

3. Import as

Import module as an alias, then access attributes through the alias.

# Import math module as 'm'
import math as m

# Access pi through the m alias

4. From … Import as

Import specific names as aliases with from <module> import <name> as <alias>

# Import sqrt as s
from math import sqrt as s

# Use alias s instead of sqrt

5. Relative Imports

If you need to import definitions from within the same top-level package, use relative imports like:

from . import module2
from .module2 import func
from ..package import module3

6. Import Library Objects

Import specific library objects rather than the entire library module:

# Import urljoin function from urllib.parse
from urllib.parse import urljoin

# Import Pi constant from math
from math import pi

Now that you’ve seen techniques for selective importing, let’s go over when you should use each approach.

When to Use Each Selective Import Technique

Choosing the right selective import syntax involves tradeoffs between convenience, readability, and namespace hygiene. Here are some guidelines on when to use each form:

Some key principles:

Best Practices for Organizing Module Imports

Follow these module import best practices to create clean, maintainable Python code:

Properly structuring imports makes large projects easier to navigate and understand for developers. Stick to these guidelines in your own code.

Handling Name Collisions When Selectively Importing

Name collisions can occur when selectively importing functions or classes of the same name from different modules. For example:

from math import sqrt
from cmath import sqrt

This is ambiguous - which sqrt() will be used?

Here are some ways to handle namespace collisions:

import math as m
import cmath as c

Then use m.sqrt() or c.sqrt() to disambiguate.

from math import sqrt as m_sqrt
from cmath import sqrt as c_sqrt
from math import sqrt
from cmath import phase

Since sqrt and phase are different names, no collision.

import math
import cmath

print(math.sqrt()) # Unambiguous
import math
import cmath

m_sqrt = math.sqrt # Rename

Carefully handling name collisions ensures that the right definitions are being accessed reliably throughout your code.

Advanced Importing with importlib

Python’s importlib module provides lower-level import functions for advanced scenarios:

import importlib

# Import module dynamically
math = importlib.import_module('math')

# Reload module

# Import specific object
sqrt = importlib.import_module('math.sqrt')

Key capabilities:

The importlib module is useful for cases like plugins or dynamic module loading where flexibility is needed.


This guide covered several techniques for selectively importing functions, classes, and objects from Python modules:

Key takeaways:

Selective importing helps organize your namespaces by loading only necessary definitions. Use these techniques to write clean, modular Python code optimized for maintainability and performance.