Skip to content

Prevent --browser from opening when no report is generated #145306

Description

@misa-j

Bug report

Bug description:

When profiling a script that does not produce HTML output, the following warning is displayed:
Warning: No functions found in profiling data. Check if sampling captured any data.
However, when the --browser option is specified, the program still attempts to open the output file in a browser.

./python -m profiling.sampling run --flamegraph --browser script.py
# script.py
print("hello world")

Suggested Fix

diff --git a/Lib/profiling/sampling/cli.py b/Lib/profiling/sampling/cli.py
index f4b31aad45b..75a21429705 100644
--- a/Lib/profiling/sampling/cli.py
+++ b/Lib/profiling/sampling/cli.py
@@ -607,6 +607,10 @@ def _open_in_browser(path):
     """
     abs_path = os.path.abspath(path)
 
+    if not os.path.exists(abs_path):
+        print(f"Warning: Could not find {abs_path}", file=sys.stderr)
+        return
+
     # For heatmap directories, open the index.html file
     if os.path.isdir(abs_path):
         index_path = os.path.join(abs_path, 'index.html')

I can open a PR with a proposed fix

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Feb 27, 2026
  2. hugovk commented on Feb 27, 2026

    @hugovk
    Member

    Thanks for the report, something like this sound sensible.

    We already get a warning:

    hello world
    Captured 1 samples in 0.00 seconds
    Sample rate: 877.29 samples/sec
    Error rate: 100.00
    Flamegraph data: 0 root functions, total samples: 0, 1 unique strings
    Warning: No functions found in profiling data. Check if sampling captured any data.
    

    Rather than checking if the file exists and printing another warning, perhaps we could have export return a success/fail code?

    collector.export(filename)
    # Auto-open browser for HTML output if --browser flag is set
    if args.format in ('flamegraph', 'heatmap') and getattr(args, 'browser', False):
    _open_in_browser(filename)

    collector.export(filename)
    # Auto-open browser for HTML output if --browser flag is set
    if args.format in ('flamegraph', 'heatmap') and getattr(args, 'browser', False):
    _open_in_browser(filename)

    Here's one of them, we'd have to check if it makes sense for the others:

    if num_functions == 0:
    print(
    "Warning: No functions found in profiling data. Check if sampling captured any data."
    )
    return

    cc @pablogsal

  3. added 5 commits that reference this issue on Feb 28, 2026
  4. misa-j commented on Feb 28, 2026

    @misa-j
    ContributorAuthor

    Yes I agree with this approach. Printing another warning feels redundant here.

  5. added 2 commits that reference this issue on Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-profilingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions