Bird
Raised Fist0
Pythonprogramming~10 mins

Import statement behavior in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Import statement behavior
Start program
Encounter import statement
Check if module is loaded
Use cached
Access module contents
Continue program execution
When Python sees an import, it checks if the module is already loaded. If yes, it uses the cached module. If not, it loads the module, then continues.
Execution Sample
Python
import math
print(math.sqrt(16))
import math
print(math.pi)
This code imports the math module twice and uses its functions and constants.
Execution Table
StepActionModule Loaded?ResultOutput
1import mathNoLoad math module
2print(math.sqrt(16))YesCall sqrt(16)4.0
3import mathYesUse cached math module
4print(math.pi)YesAccess pi constant3.141592653589793
💡 Program ends after printing results
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
mathundefinedmodule objectmodule objectmodule objectmodule object
Key Moments - 2 Insights
Why does importing the same module twice not reload it?
Because Python checks if the module is already loaded (see Step 3 in execution_table) and uses the cached version to save time.
What happens if you try to use a module before importing it?
You get an error because the module variable is undefined until the import runs (see variable_tracker start state).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 2?
A4.0
B16
CError
DNone
💡 Hint
Check the Output column at Step 2 in execution_table
At which step does Python decide to use the cached module instead of loading it again?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Module Loaded? and Result columns in execution_table
If the first import was removed, what would happen at Step 2?
Aprint would output 4.0 anyway
BError because math is not defined
Cmath module would be loaded at Step 2
DProgram would skip Step 2
💡 Hint
Refer to variable_tracker start state and key moment about using module before import
Concept Snapshot
import module_name

- Loads module if not loaded yet
- Uses cached module if already loaded
- Access module contents with module_name.member
- Importing twice does not reload
- Must import before use
Full Transcript
When Python runs a program and sees an import statement, it checks if the module is already loaded in memory. If not, it loads the module and makes it available. If yes, it uses the cached module to save time. This means importing the same module multiple times does not reload it again. You must import a module before using it, or you get an error. In the example, math is imported first, then used to calculate square root and access pi. The second import just uses the cached math module.

Practice

(1/5)
1. What happens when you use import module_name in Python?
easy
A. The module is copied into your current file.
B. Only the functions you call from the module are loaded.
C. The module code runs every time you call a function from it.
D. The entire module is loaded and its code runs once.

Solution

  1. Step 1: Understand import behavior

    When you import a module, Python loads the whole module and runs its code once.
  2. Step 2: Recognize module reuse

    After the first import, Python reuses the loaded module without running its code again.
  3. Final Answer:

    The entire module is loaded and its code runs once. -> Option D
  4. Quick Check:

    Import runs module once = A [OK]
Hint: Import runs module code once, then reuses it [OK]
Common Mistakes:
  • Thinking module code runs every time a function is called
  • Believing only used functions are loaded
  • Assuming module code is copied into current file
2. Which of the following is the correct syntax to import only the sqrt function from the math module?
easy
A. from math import sqrt
B. import math.sqrt
C. import sqrt from math
D. from sqrt import math

Solution

  1. Step 1: Recall import syntax for specific functions

    To import a specific function, use from module import function.
  2. Step 2: Match syntax to options

    from math import sqrt matches this syntax: from math import sqrt.
  3. Final Answer:

    from math import sqrt -> Option A
  4. Quick Check:

    Specific import uses 'from module import item' = A [OK]
Hint: Use 'from module import name' to import parts [OK]
Common Mistakes:
  • Using dot notation in import statement incorrectly
  • Swapping module and function names
  • Using 'import' with 'from' in wrong order
3. Consider two files:

# file1.py print('Loading file1') value = 10
# file2.py import file1 import file1 print(file1.value)

What is the output when running file2.py?
medium
A. Loading file1 Loading file1 10
B. Loading file1 10
C. 10 10
D. Error: module imported twice

Solution

  1. Step 1: Understand module import behavior

    Python runs the module code only once, even if imported multiple times.
  2. Step 2: Trace the output

    On first import, 'Loading file1' prints. Second import does nothing. Then prints value 10.
  3. Final Answer:

    Loading file1 10 -> Option B
  4. Quick Check:

    Module code runs once, reused later = B [OK]
Hint: Module code runs once, even if imported multiple times [OK]
Common Mistakes:
  • Expecting module code to run twice
  • Thinking repeated imports cause errors
  • Confusing print output order
4. What is wrong with this code?

import math from math import sqrt print(math.sqrt(16))
medium
A. You cannot import the same module twice.
B. The code will cause a NameError.
C. There is no error; the code runs and prints 4.0.
D. You must use only one import style per module.

Solution

  1. Step 1: Check import statements

    Importing the same module twice with different styles is allowed and does not cause error.
  2. Step 2: Verify function call

    Calling math.sqrt(16) works and returns 4.0.
  3. Final Answer:

    There is no error; the code runs and prints 4.0. -> Option C
  4. Quick Check:

    Multiple imports allowed; function call works = D [OK]
Hint: Multiple imports of same module are allowed [OK]
Common Mistakes:
  • Thinking multiple imports cause errors
  • Confusing import styles must be exclusive
  • Expecting NameError from this code
5. You have a module 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?
hard
A. 10, because the module is loaded once and changes persist.
B. Error, because you cannot assign to module variables.
C. 5, because the second import reloads the module resetting variables.
D. None, because the variable is not accessible after import.

Solution

  1. Step 1: Understand module import caching

    Python loads a module once and caches it; subsequent imports reuse the same module object.
  2. Step 2: Analyze variable assignment and import

    Changing config.setting to 10 modifies the cached module. The second import does not reload, so the change remains.
  3. Final Answer:

    10, because the module is loaded once and changes persist. -> Option A
  4. Quick Check:

    Module cached; changes persist = C [OK]
Hint: Module imports cache; variable changes stay across imports [OK]
Common Mistakes:
  • Assuming second import reloads module
  • Thinking module variables are immutable
  • Believing variable becomes inaccessible