Skip to content

Fix import_child_modules path resolution when CWD differs from package location#691

Open
fazalpsinfo-cmyk wants to merge 1 commit into
Neoteroi:mainfrom
fazalpsinfo-cmyk:fix/import-child-modules-path-resolution
Open

Fix import_child_modules path resolution when CWD differs from package location#691
fazalpsinfo-cmyk wants to merge 1 commit into
Neoteroi:mainfrom
fazalpsinfo-cmyk:fix/import-child-modules-path-resolution

Conversation

@fazalpsinfo-cmyk

@fazalpsinfo-cmyk fazalpsinfo-cmyk commented Jul 18, 2026

Copy link
Copy Markdown

Description

Fixes #688

import_child_modules used os.path.relpath() to convert a filesystem path into a dotted Python module path. This is incorrect when the current working directory differs from the package's location on sys.path — for example, when the application is installed as a system tool (e.g., via uv tool install) and run from any directory.

Root Cause

os.path.relpath(path) returns a path relative to the CWD. When the app is installed via uv tool install and run from ~, the path becomes something like .local/share/uv/tools/myapp/lib/python3.12/site-packages/myapp/routes, which gets converted to the invalid module path .local.share.uv.tools.myapp.lib.python3.12.site-packages.myapp.routes.

Fix

Instead of relying on CWD-relative paths, walk up the directory tree following __init__.py markers to determine the correct dotted module path. This ensures imports work correctly regardless of the current working directory.

Testing

Tested with the reproduction case from the issue. The old approach produces an incorrect dotted path (e.g., tmphbn6dhw8.myapp.routes when run from /tmp), while the new approach correctly produces myapp.routes.

Checklist

  • The fix handles the case where routes/ or controllers/ directory is a proper Python package with __init__.py
  • Falls back to bare __import__ when no package structure can be determined
  • Uses root_path.resolve() to handle symlinks

Support this project: https://buymeacoffee.com/muhamedfazalps

The function used os.path.relpath() to convert a filesystem path
into a dotted Python module path, but this is incorrect when the
current working directory differs from the package's location on
sys.path - for example, when the application is installed as a
system tool (e.g., via 'uv tool install') and run from any directory.

Instead of relying on CWD-relative paths, walk up the directory
tree following __init__.py markers to determine the correct
dotted module path. This ensures the import works regardless
of the current working directory.

Fixes: Neoteroi#688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

routes/ auto-import crashes when app is installed as a package (with uv tool)

1 participant