Python

Modules & Packages

30 flashcards · answers and spaced-repetition review in the KnowCard app

After import math, why does calling sin(math.pi) (without the math. prefix) raise NameError?
What does this print? ``` import math as m print(m.sqrt(9)) print(math.sqrt(9)) ```
What does this print? ``` pi = 1234 from math import * print(pi) ```
When you do from somemod import *, which of a module's names are NOT imported by default?
Why is from os import * considered an anti-pattern even though it saves typing the os. prefix?
What does this print? ``` import mymod # prints "loading" on import import mymod import mymod ``` (mymod has a top-level `print("loading")`)
You edit mymod.py while a Python REPL is running, then import mymod again. Why don't your changes take effect?
What does __name__ equal inside a file when it is (a) run directly as python prog.py versus (b) imported by another module?
What does this print when run as `python prog.py`? ``` def main(): print("running") if __name__ == "__main__": main() ``` And what prints if instead another file does `import prog`?
A local file is named random.py and you write import random in a sibling script. Which random gets imported, and why is that a trap?
What is sys.path and what determines whether import foo succeeds?
What turns a directory into an importable Python package, and what does its __init__.py do?
What does import effects actually execute, and does it make submodule effects.blur available?
Inside the png package's read.py, you need a function from the sibling bmp package. How do you write the relative import, and when does it fail?
You have the module name only as a string at runtime (e.g. "math"). How do you import it, since import name_var doesn't work?
Modules a.py and b.py each do import of the other at the top, then use one of its names immediately. What goes wrong?
After import math, where does the attribute lookup math.pi actually find pi?
What does from __future__ import ... do, and what's special about where it must appear?
Python splits modules into three kinds by where they live. What are they, and where is each stored?
In a fresh interpreter, what does the empty string '' at the front of sys.path mean, and where do directories from PYTHONPATH get inserted?
This is valid Python. Why? from math import ( sin, cos, tan, sinh, cosh, ) And why can't you wrap a plain import the same way?
You save a helper file as math-helper.py and try import math-helper. Why can't this module ever be imported under that name?
You have a file Utils.py and, on Windows, write import utils. Why does this fail even though Windows filenames are case-insensitive?
Inside any module you can read __file__ and __builtins__. What does each hold, and which one is missing for standard-library modules?
You run from mypackage import * expecting all its submodules to load, but only __init__.py runs. How do you actually control which submodules the star import pulls in?
When you write import foo, in what order does the interpreter search, and what exception ends the search on failure?
A directory contains both a package folder named data/ and a module file data.py. Which one does import data bind, and what happens to the other?
A file formats/png/read.py uses relative imports. Running python formats/png/read.py crashes, but python -m formats.png.read works. Why?
What makes a directory a namespace package instead of a regular package, and what trade-off does that choice bring?
The import statement is really driven by 'finders' and 'loaders'. What is each responsible for, and where are finders registered?

Start learning today

Free to start — download the app or use it in your browser.

Get it on App StoreGet it on Google Play