Conversation
When a script is profiled with ``python -m profiling.sampling run script.py`` from another directory, the script cannot import modules placed next to it, because ``_sync_coordinator._execute_script()`` executes it with the working directory (added by ``_setup_environment()`` for the module case) as ``sys.path[0]`` instead of the script's own directory. Make the script's directory importable in ``_execute_script()``, matching the behavior of ``python script.py``. Add a regression test that runs the coordinator on a script importing a sibling module.
hetaozdh
force-pushed
the
fix-sampling-script-path-main
branch
from
October 1, 2026 05:40
6c4dd14 to
16ff1ec
Compare
edvilme
reviewed
Oct 1, 2026
Co-authored-by: Eduardo Villalpando Mello <eduardo.villalpando.mello@gmail.com>
maurycy
reviewed
Oct 1, 2026
Comment on lines
+171
to
+174
| # Put directory containing the script (not the current working directory) on sys.path so that modules next to it can be imported. Similar to ``python script.py`` | ||
| script_dir = os.path.dirname(script_path) | ||
| if script_dir not in sys.path: | ||
| sys.path.insert(0, script_dir) |
Contributor
There was a problem hiding this comment.
What do you think about this edge case?
2026-10-01T12:57:30.615506000+0200 maurycy@gimel /Users/maurycy/work/cpython-pr158548 (pr158548 91c5ab3?) % tree w
w
├── link.py -> sub/where.py
└── sub
├── helper.py
└── where.py
2 directories, 3 files
2026-10-01T12:57:32.453682000+0200 maurycy@gimel /Users/maurycy/work/cpython-pr158548 (pr158548 91c5ab3?) % sudo ./python.exe -m profiling.sampling run w/link.py
Traceback (most recent call last):
File "<frozen runpy>", line 201, in _run_module_as_main
File "<frozen runpy>", line 87, in _run_code
File "/Users/maurycy/work/cpython-pr158548/Lib/profiling/sampling/_sync_coordinator.py", line 261, in <module>
main()
~~~~^^
File "/Users/maurycy/work/cpython-pr158548/Lib/profiling/sampling/_sync_coordinator.py", line 254, in main
_execute_script(script_path, script_args, cwd)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/maurycy/work/cpython-pr158548/Lib/profiling/sampling/_sync_coordinator.py", line 201, in _execute_script
exec(code, main_module.__dict__)
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/maurycy/work/cpython-pr158548/w/link.py", line 1, in <module>
import helper
ModuleNotFoundError: No module named 'helper'
2026-10-01T12:57:43.707720000+0200 maurycy@gimel /Users/maurycy/work/cpython-pr158548 (pr158548 91c5ab3?) % ./python.exe -VV
Python 3.16.0a0 (heads/pr158548:91c5ab3e096, Oct 1 2026, 10:22:50) [Clang 21.0.0 (clang-2100.3.34.2)]I'm wondering if this might be an approach to mimic:
Lines 192 to 197 in 763b6ed
… sys.path ``python script.py`` resolves symlinks when computing ``sys.path[0]``, so a script reached through a symlink (``link.py -> sub/where.py``) imports modules from the real script's directory. Apply ``os.path.realpath()`` before taking the directory name, and make sure the result is placed at the front of ``sys.path`` even if it was already listed. Add a regression test for a symlinked script.
hetaozdh
force-pushed
the
fix-sampling-script-path-main
branch
from
October 1, 2026 17:06
d9d8e40 to
c8242dd
Compare
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.
Fixes gh-158540.
When a script is executed with
python -m profiling.sampling run script.pyfrom another directory, the script cannot import modules placed next to it:
_sync_coordinator._execute_script()runs it with the working directory(added by
_setup_environment()for the-mcase) assys.path[0]instead of the script's own directory.
This change adds the script's directory to
sys.pathin_execute_script(),matching the behavior of
python script.py.Added a regression test in
Lib/test/test_profiling/test_sampling_profiler/test_sync_coordinator.pythat runs the coordinator on a script importing a sibling module, plus a
Misc/NEWS.dentry.Tests:
./python -m test test_profiling./python Tools/patchcheck/patchcheck.py