Fix import_child_modules path resolution when CWD differs from package location#691
Open
fazalpsinfo-cmyk wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #688
import_child_modulesusedos.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 onsys.path— for example, when the application is installed as a system tool (e.g., viauv 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 viauv tool installand 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__.pymarkers 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.routeswhen run from/tmp), while the new approach correctly producesmyapp.routes.Checklist
routes/orcontrollers/directory is a proper Python package with__init__.py__import__when no package structure can be determinedroot_path.resolve()to handle symlinksSupport this project: https://buymeacoffee.com/muhamedfazalps