Skip to content

[3.15] gh-158540: Add the profiled script's directory to sys.path (GH-158548) - #158844

Merged
pablogsal merged 1 commit into
python:3.15from
miss-islington:backport-3f02aab-3.15
Oct 5, 2026
Merged

pablogsal merged 1 commit into
python:3.15from
miss-islington:backport-3f02aab-3.15

Conversation

@miss-islington

Copy link
Copy Markdown
Contributor

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

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

…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
pablogsal enabled auto-merge (squash) October 5, 2026 13:53
@pablogsal
pablogsal merged commit f52d831 into python:3.15 Oct 5, 2026
100 of 102 checks passed
@miss-islington
miss-islington deleted the backport-3f02aab-3.15 branch October 5, 2026 14:50
pablogsal added a commit to pablogsal/cpython that referenced this pull request Oct 5, 2026
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.

3 participants