Use 'import module' or 'from module import'?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
When programming in Python, importing modules is an essential practice that allows developers to harness pre-existing code and functionality, thus enhancing productivity and code maintainability. The two most common forms of importing modules are import module
and from module import
. Each form serves different purposes and is suited to different scenarios. This article presents a detailed exploration of these importing mechanisms, supported by examples and technical explanations, to aid developers in making informed choices.
Importing Modules in Python
Python provides the capability to import modules, which are files containing Python code that can define functions, classes, and variables. By importing modules, developers can leverage a wealth of pre-written code to extend their own applications.
The import module
Approach
Using the import module
command, a module is loaded and its namespace remains separate from the caller’s namespace. This means all the entities within the module need to be accessed with a prefix of the module's name.
- Clarity and Readability: It is apparent which module a function or class originates from, facilitating code readability.
- Namespace Management: Different imported modules maintain separate namespaces, reducing name clashes.
- Verbose Code: Frequent use of module prefixes can make code more verbose, especially when using multiple functions from the same module.
- Conciseness: Direct access to functions or objects leads to less verbose code when multiple functions from a module are used repeatedly.
- Reduced Overhead: Importing only necessary parts of a module can reduce memory usage slightly.
- Namespace Pollution: Direct imports can lead to overwriting existing names in the local namespace, introducing bugs.
- Ambiguity: It is not immediately clear from the code where an imported object originates, which can affect code comprehension.
- Wildcard Imports:
- The use of
from module import *is generally discouraged as it imports all module contents into the local namespace, increasing the likelihood of name conflicts.
- Aliasing:
- Python allows aliasing modules or functions using
import module as alias, which can enhance readability and resolve name conflicts.
- Performance:
- While module importation is generally efficient, consider lazy loading in scenarios with significant startup time concerns.
Related reading
- use kafka-producer-perf-test.sh how to set producer config at kafka_2.10-0.8.2.0
- Use of Finalize/Dispose method in C
- use trial.suggest_int to pick values from given list in optuna, just like trial.suggest_categorical does
- User analysis based on their facebook profile?
- Use of *args and **kwargs
- Use of args and kwargs
- using 10 MB of memory for four billion integers about finding the optimized block size
- Using A to solve Travelling Salesman

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.