We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Modules, Packaging and Tooling step 2 of 41
Find the Folder Python Imports a Package From
You run import mypkg and want to know which folder on sys.path it comes
from. Write a small model of the search Python runs. It works on a pretend
filesystem made of dictionaries, because the grader runs one file of code and
cannot create real folders.
def resolve_module(
sys_path: Sequence[str],
filesystem: Mapping[str, frozenset[str]],
module: str,
) -> str | None:
For example, with sys_path = ["", "/venv/site-packages"], a mypkg package
in the current folder and another in site-packages, the answer is "". The
current folder comes first, so its copy wins.
The pretend filesystem
sys_pathlists folders in search order.filesystemmaps each folder's path to the names directly inside it. A name is a subfolder when its joined path is also a key infilesystem. Otherwise it is a file.- Join a folder and a name with
f"{parent}/{name}". Keys never end in/. - The key
""stands for the current folder, so its subfoldermypkghas the key"/mypkg". moduleis one plain name such asmypkg, never a dotted name.- Names ending in
.py, and__init__.py, are always files.
The search rules
For each entry in sys_path, in order:
- Skip the entry if it is not a key in
filesystem. - If the entry lists
moduleand its joined path is a folder, look inside. If the folder holds__init__.py, it is a regular package, so return the entry now. If not, remember the entry as a namespace portion and go on to step 3. - If the entry lists
module + ".py", return the entry. A module file beats a namespace portion in the same folder, and a regular package from step 2 beats the module file. - Otherwise move on to the next entry.
When the loop ends, return the first namespace portion you remembered.
If there was none, return None. "" is a real answer, the current folder,
so test for a remembered portion with is None.
A regular package or module found later wins over a namespace portion found
earlier. If /a/pkg has no __init__.py and /b/pkg has one, searching
["/a", "/b"] for pkg returns "/b". If both are namespace portions, the
answer is "/a". Real Python would combine both portions into one namespace
package, and returning the first one is how this exercise reports that.
The model leaves out the module cache, compiled extension modules, zip files
and import hooks. Do not change the inputs. Your code must pass
mypy --strict, so check that filesystem.get(entry) is not None before
you look inside it.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.