Skip to content

gh-148932: Fix profiling.sampling on Windows virtual environments#150541

Open
edvilme wants to merge 5 commits into
python:mainfrom
edvilme:profiling-windows-venv
Open

gh-148932: Fix profiling.sampling on Windows virtual environments#150541
edvilme wants to merge 5 commits into
python:mainfrom
edvilme:profiling-windows-venv

Conversation

@edvilme
Copy link
Copy Markdown
Contributor

@edvilme edvilme commented May 27, 2026

Currently, it is not possible to run profiling.sampling from a Windows venv because virtual environments use shims to launch the original interpreter, but the shim itself isn't a real Python process.

Fix

Added _resolve_venv_child function to detect when the profiler is started from a virtual environment process on Windows, and automatically redirect it to the real child Python process. This prevents profiling against the shim instead of the actual Python process.

The SampleProfiler class uses the resolved child PID when initializing.

Before

(venv) PS C:\Users\user\Documents\project> python -m profiling.sampling run .\test.py
Running profiling.sampling from virtualenv on Windows platform is not supported

After

(venv) PS C:\Users\user\Documents\project> python -m profiling.sampling run .\test.py
Profile Stats:
       nsamples   sample%  tottime (ms)    cumul%  cumtime (ms)  filename:lineno(function)
        465/465      24.6       465.000      24.6       465.000  test.py:59(bubble_sort)
        452/589      24.0       452.000      31.2       589.000  test.py:27(fibonacci_recursive)
        400/400      21.2       400.000      21.2       400.000  test.py:60(bubble_sort)
        227/227      12.0       227.000      12.0       227.000  test.py:72(matrix_multiply)
        130/130       6.9       130.000       6.9       130.000  test.py:58(bubble_sort)
         95/123       5.0        95.000       6.5       123.000  test.py:25(fibonacci_recursive)
          27/27       1.4        27.000       1.4        27.000  test.py:71(matrix_multiply)
          23/23       1.2        23.000       1.2        23.000  test.py:26(fibonacci_recursive)
          19/31       1.0        19.000       1.6        31.000  test.py:23(fibonacci_recursive)
            9/9       0.5         9.000       0.5         9.000  test.py:34(is_prime)
            7/7       0.4         7.000       0.4         7.000  test.py:35(is_prime)
            6/6       0.3         6.000       0.3         6.000  test.py:84(compute_collatz)
            4/8       0.2         4.000       0.4         8.000  random.py:342(Random.randint)
            3/3       0.2         3.000       0.2         3.000  random.py:339(Random.randint)
            3/3       0.2         3.000       0.2         3.000  test.py:80(compute_collatz)

Legend:
  nsamples: Direct/Cumulative samples (direct executing / on call stack)
  sample%: Percentage of total samples this function was directly executing
  tottime: Estimated total time spent directly in this function
  cumul%: Percentage of total samples when this function was on the call stack
  cumtime: Estimated cumulative time (including time in called functions)
  filename:lineno(function): Function location and name

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 27, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32877885 | 📁 Comparing c91e40e against main (9242700)

  🔍 Preview build  

2 files changed
± library/profiling.sampling.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant