Import statement behavior in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use import statements in Python, it's important to understand how the time to load modules grows as the number of imports increases.
We want to know how the program's start-up time changes when importing many modules.
Analyze the time complexity of the following code snippet.
import module1
import module2
import module3
# Imagine many more imports here
print("Modules imported")
This code imports several modules before running the main program.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loading and initializing each imported module.
- How many times: Once for each import statement executed.
Each import adds a fixed amount of work to load that module.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 module loads |
| 100 | About 100 module loads |
| 1000 | About 1000 module loads |
Pattern observation: The total work grows directly with the number of imports.
Time Complexity: O(n)
This means the time to import modules grows in a straight line as you add more imports.
[X] Wrong: "Importing many modules happens instantly and does not affect program speed."
[OK] Correct: Each import requires loading and running code once, so more imports mean more work and longer start-up time.
Understanding how imports affect program start-up helps you write efficient code and manage dependencies well.
What if we used import inside a function instead of at the top? How would the time complexity change when calling that function multiple times?
Practice
import module_name in Python?Solution
Step 1: Understand import behavior
When you import a module, Python loads the whole module and runs its code once.Step 2: Recognize module reuse
After the first import, Python reuses the loaded module without running its code again.Final Answer:
The entire module is loaded and its code runs once. -> Option DQuick Check:
Import runs module once = A [OK]
- Thinking module code runs every time a function is called
- Believing only used functions are loaded
- Assuming module code is copied into current file
sqrt function from the math module?Solution
Step 1: Recall import syntax for specific functions
To import a specific function, usefrom module import function.Step 2: Match syntax to options
from math import sqrt matches this syntax:from math import sqrt.Final Answer:
from math import sqrt -> Option AQuick Check:
Specific import uses 'from module import item' = A [OK]
- Using dot notation in import statement incorrectly
- Swapping module and function names
- Using 'import' with 'from' in wrong order
# file1.py
print('Loading file1')
value = 10# file2.py
import file1
import file1
print(file1.value)What is the output when running
file2.py?Solution
Step 1: Understand module import behavior
Python runs the module code only once, even if imported multiple times.Step 2: Trace the output
On first import, 'Loading file1' prints. Second import does nothing. Then prints value 10.Final Answer:
Loading file1 10 -> Option BQuick Check:
Module code runs once, reused later = B [OK]
- Expecting module code to run twice
- Thinking repeated imports cause errors
- Confusing print output order
import math
from math import sqrt
print(math.sqrt(16))Solution
Step 1: Check import statements
Importing the same module twice with different styles is allowed and does not cause error.Step 2: Verify function call
Callingmath.sqrt(16)works and returns 4.0.Final Answer:
There is no error; the code runs and prints 4.0. -> Option CQuick Check:
Multiple imports allowed; function call works = D [OK]
- Thinking multiple imports cause errors
- Confusing import styles must be exclusive
- Expecting NameError from this code
config.py with a variable setting = 5. In your main program, you do:import config
config.setting = 10
import config
print(config.setting)What will be printed and why?
Solution
Step 1: Understand module import caching
Python loads a module once and caches it; subsequent imports reuse the same module object.Step 2: Analyze variable assignment and import
Changingconfig.settingto 10 modifies the cached module. The second import does not reload, so the change remains.Final Answer:
10, because the module is loaded once and changes persist. -> Option AQuick Check:
Module cached; changes persist = C [OK]
- Assuming second import reloads module
- Thinking module variables are immutable
- Believing variable becomes inaccessible
