[3.15] gh-158540: Add the profiled script's directory to sys.path (GH-158548) - #158844
Merged
Merged
Conversation
…honGH-158548) * pythongh-158540: Add the profiled script's directory to sys.path 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. * Update _sync_coordinator.py Comment simplified Co-authored-by: Eduardo Villalpando Mello <eduardo.villalpando.mello@gmail.com> * pythongh-158540: Resolve symlinks when adding the script directory to 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. --------- (cherry picked from commit 3f02aab) Co-authored-by: he_tao <53343436+hetaozdh@users.noreply.github.com> Co-authored-by: Eduardo Villalpando Mello <eduardo.villalpando.mello@gmail.com>
pablogsal
enabled auto-merge (squash)
October 5, 2026 13:53
pablogsal
added a commit
to pablogsal/cpython
that referenced
this pull request
Oct 5, 2026
…o sys.path (pythonGH-158548) (python#158844)" This reverts commit f52d831.
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.
run script.pydoes not put the script's directory on sys.path, so sibling imports fail #158540: Add the profiled script's directory to sys.pathWhen a script is profiled with
python -m profiling.sampling run script.pyfrom another directory, the script cannot import modulesplaced next to it, because
_sync_coordinator._execute_script()executes it with the working directory (added by
_setup_environment()for the module case) assys.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.
Co-authored-by: Eduardo Villalpando Mello eduardo.villalpando.mello@gmail.com
run script.pydoes not put the script's directory on sys.path, so sibling imports fail #158540: Resolve symlinks when adding the script directory to sys.pathpython script.pyresolves symlinks when computingsys.path[0], soa script reached through a symlink (
link.py -> sub/where.py) importsmodules 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.patheven if it was already listed.Add a regression test for a symlinked script.
(cherry picked from commit 3f02aab)
Co-authored-by: he_tao 53343436+hetaozdh@users.noreply.github.com
Co-authored-by: Eduardo Villalpando Mello eduardo.villalpando.mello@gmail.com