@@ -19,7 +19,7 @@ def clean_bytecode():
1919 count = 0
2020 # Walk the directory avoiding hidden directories (e.g. .git, .venv, .nox)
2121 for root , dirs , files in os .walk ('.' ):
22- dirs [:] = [d for d in dirs if not d .startswith ('.' )]
22+ dirs [:] = [d for d in dirs if not d .startswith ('.' ) or d == '.venv-profiler' ]
2323 if '__pycache__' in dirs :
2424 shutil .rmtree (os .path .join (root , '__pycache__' ))
2525 dirs .remove ('__pycache__' )
@@ -207,6 +207,13 @@ def run_master(iterations, target_module, cpu=0, csv_path=None, clear_cache=True
207207 print (f"Error in worker process:\n { e .stderr } " , file = sys .stderr )
208208 raise e
209209
210+ if iterations > 1 :
211+ times = times [1 :]
212+ memories = memories [1 :]
213+ rss_memories = rss_memories [1 :]
214+ iterations -= 1
215+ print ("Discarded the first iteration as a cache burn-in run." )
216+
210217 # Write CSV if requested
211218 if csv_path :
212219 with open (csv_path , "w" , newline = "" , encoding = "utf-8" ) as f :
@@ -356,6 +363,39 @@ def validate_module_name(module_name):
356363 return module_name
357364
358365 def find_module_from_package (pkg ):
366+ import importlib .metadata
367+
368+ # 1. Try to use importlib.metadata.files (works for standard installations from PyPI/wheels)
369+ try :
370+ files = importlib .metadata .files (pkg )
371+ if files :
372+ init_files = [str (f ) for f in files if str (f ).endswith ('__init__.py' ) and '__pycache__' not in str (f ) and not str (f ).startswith ('tests/' )]
373+ if init_files :
374+ shortest_init = min (init_files , key = lambda p : len (p .split ('/' )))
375+ parts = shortest_init .split ('/' )[:- 1 ]
376+ mod = '.' .join (parts )
377+ if importlib .util .find_spec (mod ):
378+ return mod
379+ except Exception :
380+ pass
381+
382+ # 2. Try setuptools.find_namespace_packages() in current directory (works for editable installs in source trees)
383+ try :
384+ import setuptools
385+ import os
386+ if os .path .exists ('setup.py' ) or os .path .exists ('pyproject.toml' ):
387+ pkgs = setuptools .find_namespace_packages (where = '.' )
388+ for p in sorted (pkgs , key = len ):
389+ if p .startswith ('tests' ):
390+ continue
391+ path = p .replace ('.' , os .sep )
392+ if os .path .isfile (os .path .join (path , '__init__.py' )):
393+ if importlib .util .find_spec (p ):
394+ return p
395+ except Exception :
396+ pass
397+
398+ # 3. Fallback to basic string manipulation heuristics
359399 candidates = [
360400 pkg .replace ('-' , '.' ),
361401 '.' .join (pkg .split ('-' )[:- 1 ]) + '_' + pkg .split ('-' )[- 1 ] if '-' in pkg else pkg ,
0 commit comments