def generate_imports_for_referenced_namespaces(<EOL>backend, namespace, insert_type_ignore=False):<EOL>
imported_namespaces = namespace.get_imported_namespaces(consider_annotation_types=True)<EOL>if not imported_namespaces:<EOL><INDENT>return<EOL><DEDENT>type_ignore_comment = TYPE_IGNORE_COMMENT if insert_type_ignore else "<STR_LIT>"<EOL>backend.emit('<STR_LIT>')<EOL>with backend.indent():<EOL><INDENT>backend.emit('<STR_LIT>')<EOL>with backend.indent():<EOL><INDENT>for ns in imported_namespaces:<EOL><INDENT>backend.emit(fmt_namespace(ns.name) + '<STR_LIT:U+002C>')<EOL><DEDENT><DEDENT>backend.emit('<STR_LIT:)>')<EOL><DEDENT>backend.emit('<STR_LIT>')<EOL>with backend.indent():<EOL><INDENT>for ns in imported_namespaces:<EOL><INDENT>backend.emit('<STR_LIT>'.format(<EOL>namespace_name=fmt_namespace(ns.name),<EOL>type_ignore_comment=type_ignore_comment<EOL>))<EOL><DEDENT><DEDENT>backend.emit()<EOL>
Both the true Python backend and the Python PEP 484 Type Stub backend have
to perform the same imports.

:param insert_type_ignore: add a MyPy type-ignore comment to the imports in
    the except: clause.