The program below stops with an error. Which statement correctly describes the problem?
import maths
print(maths.sqrt(25))
Module names must match the file name exactly.
Modules and Packages · Exercise 3 of 4
Each Python program has an import mistake: an exception at run time or a logical bug such as missing a __main__ guard or relying on a stale cached module. Choose the option that names the real problem.
The program below stops with an error. Which statement correctly describes the problem?
import maths
print(maths.sqrt(25))
Module names must match the file name exactly.
The program below stops with an error. Which statement correctly describes the problem?
from math import square_root
print(square_root(49))
The module is found, but the requested name is not in it, so the error is ImportError ("cannot import name").
The program below stops with an error. Which statement correctly describes the problem?
import math
print(sqrt(16))
import module does not copy the module's names into your namespace.
The program below stops with an error. Which statement correctly describes the problem?
import math
print(math.squareroot(4))
The import succeeds; the missing name is found only when the attribute is looked up.
The program below stops with an error. Which statement correctly describes the problem?
import math
math = 3.14 # "the value of pi"
print(math.sqrt(4))
A module name is an ordinary variable and can be rebound by accident.
The program below stops with an error. Which statement correctly describes the problem?
from os import *
f = open("notes.txt", "w")
f.write("hello")
os.open(path, flags) expects an int as its second argument. A wildcard import silently shadowed a built-in.
The program below stops with an error. Which statement correctly describes the problem?
import math.sqrt
print(math.sqrt(9))
Dotted import paths name packages and modules, never functions.
This file is run directly with python main.py. It stops with an error. Which statement correctly describes the problem?
from . import helpers
print(helpers.greet())
Python reports "attempted relative import with no known parent package".
The program should print only 10. Which statement correctly identifies the bug in the module it imports?
import sys, tempfile, pathlib
folder = pathlib.Path(tempfile.mkdtemp())
(folder / "lf_calc.py").write_text(
"def add(a, b):\n return a + b\n"
"print('Testing add:', add(2, 3))\n") # the module's own quick test
sys.path.insert(0, str(folder))
from lf_calc import add
print(add(4, 6))
Importing executes the whole file once. Guard demo and test code.
The settings file is changed to VALUE = 2 and imported again, but the program still prints 1 twice. Which statement correctly identifies the bug?
import sys, tempfile, pathlib
folder = pathlib.Path(tempfile.mkdtemp())
settings = folder / "lf_settings.py"
settings.write_text("VALUE = 1\n")
sys.path.insert(0, str(folder))
import lf_settings
print(lf_settings.VALUE, end=" ")
settings.write_text("VALUE = 2\n") # edit the module file
import lf_settings # "import it again"
print(lf_settings.VALUE)
Imports are cached for the life of the process. reload re-executes the file (or restart the program).
Timed online tests with instant scores and explanations.