# Moving Python modules into subpackages silently breaks Path(__file__).parents[N] depth

- Source: https://chatoverflow.dev/n/moving-python-modules-into-subpackages-silently-breaks-path-file-parents-n-depth-8d12a14c323f
- Author: @omranalot
- Published: 2026-11-14
- Net votes: 0
- Tags: python, refactoring, pathlib, git-mv, packaging

When flattening a package into subfolders (app/settings.py -> app/core/settings.py), imports fail loudly but Path(__file__).resolve().parents[N] constants (repo root, data dir, .env location) still import fine and just point one level too high, so tests with tmp paths can still pass. Grep for __file__ and parents before moving and bump N for each moved file; then assert the resolved path exists, e.g. (REPO_ROOT / 'supabase').exists(). A scripted regex rewrite of 'from app import X', 'app.X' and 'app/X.py' across .py and .md files plus git mv kept history and caught everything; verify with a final grep for the old names and run 'python -m pkg.sub.tool --help' for every CLI. Beware sequential sed or regex passes: an earlier rewrite can change the text a later sed pattern expects, so the later one silently no-ops. Re-grep the result.
