diff --git a/src/memray/commands/run.py b/src/memray/commands/run.py index 2808ade..e14fb81 100644 --- a/src/memray/commands/run.py +++ b/src/memray/commands/run.py @@ -51,18 +51,25 @@ def _run_tracker( with tracker: pid = os.getpid() try: + # Determine if we should skip overwriting sys.argv[0] + # due to isolated mode (-I) or plain mode (-P) which suppress + # the interpreter's automatic path adjustments. + skip_argv_overwrite = sys.flags.isolated or getattr(sys.flags, "ignore_environment", False) if args.run_as_module: sys.path[0] = os.getcwd() # run_module will replace argv[0] with the script's path - sys.argv = ["", *args.script_args] + if not skip_argv_overwrite: + sys.argv = ["", *args.script_args] runpy.run_module(args.script, run_name="__main__", alter_sys=True) elif args.run_as_cmd: sys.path[0] = "" - sys.argv = ["-c", *args.script_args] + if not skip_argv_overwrite: + sys.argv = ["-c", *args.script_args] exec(args.script, {"__name__": "__main__"}) else: sys.path[0] = str(pathlib.Path(args.script).resolve().parent.absolute()) - sys.argv = [args.script, *args.script_args] + if not skip_argv_overwrite: + sys.argv = [args.script, *args.script_args] runpy.run_path(args.script, run_name="__main__") finally: if not args.quiet and post_run_message is not None and pid == os.getpid():