@@ -286,10 +286,12 @@ def generate_module(module_name: str, output_dir: str, indent_size: int = 4, max
286286
287287 module = import_module (module_name )
288288 generated_root = Path (output_dir )
289+ module_root = module .__name__ .split ('.' )[- 0 ]
289290 module_base_name = module .__name__ .split ('.' )[- 1 ]
290291 self_include = f'"{ module_name .replace (dot , slash )} .h"'
292+ include_root = "/" .join (module_name .split ("." )[:- 1 ])
291293 qualified_module_name = "_" .join (module .__name__ .split ('.' )[:- 1 ]) + "_" + module_base_name
292- namespace = '::' .join (module_name .split ('.' )[:- 1 ])
294+ namespace = "::" .join (module_name .split ("." )[:- 1 ])
293295 guard = f"{ namespace .upper ().replace ('::' , '_' )} _{ module_base_name .upper ()} _H"
294296
295297 if not generated_root .exists ():
@@ -315,12 +317,13 @@ def generate_module(module_name: str, output_dir: str, indent_size: int = 4, max
315317 includes = includes .union (struct_includes )
316318 usings = usings .union (struct_usings )
317319
318-
319320 imports = []
320- for include in (i for i in includes if namespace in i ):
321+ for include in (i for i in includes if i . startswith ( '"' + module_root ) and include_root not in i ):
321322 import_parts = include .split (slash )
322323 import_parts .insert (- 1 , "__pybind__" )
323- imprt = '.' .join (import_parts ).replace ('#include ' , '' ).replace ('.h' , '' )
324+ import_qualifier = "_" .join (import_parts [:- 2 ]).strip ('"' )
325+ import_parts [- 1 ] = import_qualifier + "_" + import_parts [- 1 ].replace (".h" , "" )
326+ imprt = "." .join (import_parts )
324327 imports .append (f"{ indent } py::module_::import({ imprt } );" )
325328
326329 includes = [f"#include { i } " for i in
0 commit comments