[ norboten ]
python · lab python-05

The Module That Shadows the Standard Library

· about 35 minutes · runs on ubuntu-26.04-devops · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

/opt/digest/digest.py reads the day's events from /srv/digest/events.json and writes a one-line digest to /var/lib/digest/today.txt. digest.service runs it after boot.

Since a colleague added a small helper for the internal calendar, every run fails with a traceback that makes no sense: AttributeError: module 'calendar' has no attribute 'monthrange'. Somebody added WorkingDirectory=/opt/digest to the unit last week hoping it would help; it changed nothing.

What is expected, and graded — the grader runs digest.py itself, through the DIGEST_EVENTS and DIGEST_OUT variables the script already reads:

  1. Started from /opt/digest, the program writes the digest and exits 0.
  2. Started from any other directory, it writes exactly the same digest.
  3. The standard library modules it uses are the standard library's, not the program's own files: importing calendar, json or logging in a program started from /opt/digest gives the module from Python's own library.
  4. digest.service succeeds and the digest holds today's events — and still does after a reboot.

You have root through sudo. The helper's own function must keep working: the digest names the number of days in the month.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_runs_from_its_own_directoryKeep a program's own modules from standing in for the standard library
02_runs_from_anywhereRun a program from a systemd unit without depending on the caller's directory
03_the_stdlib_is_not_shadowedKnow where Python looks for a module, and in which order
04_the_service_writes_its_digestRun a program from a systemd unit without depending on the caller's directory

Start it

  1. 2Labs
  2. python-05select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

4 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Three of them, to answer here:

Question 1

`/opt/digest/digest.py` starts with `import calendar`, and `/opt/digest/calendar.py` exists. The program is started with `cd /tmp && python3 /opt/digest/digest.py`. Which file is imported?

Question 2

What does this print?

import pathlib, subprocess, sys, tempfile
d = pathlib.Path(tempfile.mkdtemp())
(d / "calendar.py").write_text("VALUE = 1\n")
(d / "app.py").write_text("import calendar; print(hasattr(calendar, 'monthrange'))\n")
print(subprocess.run([sys.executable, str(d / "app.py")],
                     cwd="/", capture_output=True, text=True).stdout.strip())
Question 3

Which of these modules cannot be shadowed by a file of the same name beside your script?