From 6b5589840ce7479961d857d4e7c80143ec29fdf7 Mon Sep 17 00:00:00 2001 From: Adrien POUYET Date: Tue, 16 Apr 2019 13:33:44 +0200 Subject: [PATCH] allow the usage of __file__ in the script and make sure its parent directory is in the path --- memory_profiler.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/memory_profiler.py b/memory_profiler.py index 9b59b8f..4e75a83 100644 --- a/memory_profiler.py +++ b/memory_profiler.py @@ -1119,7 +1119,15 @@ def choose_backend(new_backend=None): def exec_with_profiler(filename, profiler, backend, passed_args=[]): from runpy import run_module builtins.__dict__['profile'] = profiler - ns = dict(_CLEAN_GLOBALS, profile=profiler) + ns = dict(_CLEAN_GLOBALS, + profile=profiler, + # Make sure the __file__ variable is usable + # by the script we're profiling + __file__=filename) + # Make sure the script's directory in on sys.path + # credit to line_profiler + sys.path.insert(0, os.path.dirname(script_filename)) + _backend = choose_backend(backend) sys.argv = [filename] + passed_args try: