Skip to content

Python: allow profiled scripts to import sibling modules - #183

Open
rioyu123 wants to merge 1 commit into
NVIDIA:devfrom
rioyu123:codex/nvtx-script-import-path
Open

rioyu123 wants to merge 1 commit into
NVIDIA:devfrom
rioyu123:codex/nvtx-script-import-path

Conversation

@rioyu123

Copy link
Copy Markdown

python script.py puts the script's directory first on sys.path, so it can import modules that sit next to it. python -m nvtx script.py uses runpy.run_path, which does not do this for a file path. A sibling import can therefore fail with ModuleNotFoundError or pick up a different module from the current directory.

This change adds the script's absolute directory to the front of sys.path before execution, following the standard library's cProfile and profile command-line tools. It leaves argument parsing and profiling behavior unchanged.

Testing:

  • Two real-subprocess regressions cover absolute and relative script paths. A conflicting module in the working directory verifies that the script's own sibling takes precedence.
  • Linux, CPython 3.12: the full Python suite passes with and without --enable-injection (4,469 passed in each run).
  • Windows, macOS, and other Python versions were not tested locally.

Related: #181 fixes argument forwarding, not import paths. Both changes touch the CLI and add test_main.py, so whichever merges second may need a small rebase. This PR does not include #181's changes.

Signed-off-by: Rio Yu <52408936+rioyu123@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant