Package: py-imports
Be aware about imports meta informationSource Code:https://github.com/andresbena19/py-importsRequirementsPython 3.7+py-imports stands on the shoulders of giants:ast — Abstract Syntax Treesto traverse python code.Installation$pipinstallpy-imports---> 100%All it's ready to beginExampleIntrospect itCreate a filemain.pywith:importloggingfrompy_imports.managerimportPyImportsfrom.parse_local_dirimportfile_imports_from_dir_onemyself="main.py"# Let's introspect myselfwithPyImports()asmanager:imports_file=manager.get_imports(myself)# Now you have access to the imports used in each fileimports_file{'main.py':<py_imports.base.models.ImportsCollectionFileobjectat0x10b889220>}# Get details about the absolute, relative and standard imports in the filecollector_object=imports_file.get(myself)absolute_imports=collector_object.absolute_importsrelative_imports=collector_object.relative_importsstandard_imports=collector_object.importsGet meta information about absolute imports...absolute_imports# Absolute imports#  --- from py_imports.manager import PyImports ---# If we introspect the object, we will get the followingexample_abs_import=absolute_imports[0]example_abs_import.children->['PyImports']example_abs_import.parent->'py_imports.manager'example_abs_import.statement->'from py_imports.manager import PyImports'example_abs_import.level->0example_abs_import.line->2Get meta information about relative imports...relative_imports# relative imports#  --- from .parse_local_dir import file_imports_from_dir_one ---# If we introspect the object, we will get the followingexample_relative_import=relative_imports[0]example_abs_import.children->['file_imports_from_dir_one']example_abs_import.children_unused->['file_imports_from_dir_one']example_abs_import.parent->'parse_local_dir'example_abs_import.statement->'from .parse_local_dir import file_imports_from_dir_one'example_abs_import.level->1example_abs_import.line->3Get meta information about standard imports ...standard_imports# standard imports#  --- import logging ---# If we introspect the object, we will get the followingexample_standard_import=standard_imports[0]example_standard_import.children->['logging']example_standard_import.children_unused->['logging']example_standard_import.statement->'import logging'example_standard_import.line->1Now you know more about you...FeaturesClassify the imports found into three groupsThe util allow identifying and group the imports according  ...relative imports, absolu...Python Abstract GrammarThe util allow identifying and group the imports according to the abstract grammar defined with python...
 | Import(alias* names)
 | ImportFrom(identifier? module, alias* names, int? level)Import typesRelative ImportsRelative imports use leading dots. A single leading dot indicates a relative import, starting with the current package.
Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first.Schema syntaxRelative imports must always usefrom <> import;import <>is always absolute.pydocs:https://docs.python.org/3/reference/import.html#package-relative-importsMetadata will be abstracted inRelativeImportStatementobjects.Ex.from.moduleYimportspamfrom.moduleYimportspamashamfrom.importmoduleYfrom..subpackage1importmoduleYfrom..subpackage2.moduleZimporteggsfrom..moduleAimportfoofrom...packageimportbarfrom...sysimportpathAbsolute ImportsAbsolute import involves full path i.e., from the project’s root folder to the desired module. An absolute import state that the resourceto be imported using its full path from the project’s root folder.Schema syntaxAbsolute imports may use either theimport <>orfrom <> import <>syntax, but relative imports may only use the second form.PEP328:https://www.python.org/dev/peps/pep-0328/Metadata will be abstracted inAbsoluteImportStatementobjects.Ex.frommoduleYimportspamfrommoduleYimportspamasham# ORimportXXX.YYY.ZZZStandard ImportsStandard imports will be introspected and the data about it will be saved in an
object namedImportStatement.Schema syntaxstandard imports use  theimport <>syntax.PEP328:https://www.python.org/dev/peps/pep-0328/Metadata will be abstracted inImportStatementobjects.Ex.importmoduleYimportmoduleXIf the imports are being usedIf some child it's not used in an import  ...children_unused...Unused importsIf some child it's not used in an import, this will be added inchildren_unusedattribute in every concrete implementation that represent an imports.from..subpackage1importmoduleY,moduleZdeffoo()->moduleZ:passIn this case the relative importfrom ..subpackage1 import moduleY, moduleZhas a child that is not used in the file....# After introspect the filerelative_imports=imports_file.relative_importsrelative_imports[0].children_unused->["moduleY"]# But the total of children present in this filerelative_imports[0].children->["moduleY","moduleZ"]it's usedpyflakesto determine the unused imports, because follow the same philosophy to get the
information just using a static analysis.If the imports are located inside an inner scope ex. function, class, etc.If the position of the import statement ...in_inner_scope...Imports in inner scopesIf some imports are located inside an inner scope, the import object will contain a boolean field
namedin_inner_scopeindicating that is located outside his default position (the top of the file or in the global scope),
also will be included an attribute namedouter_parent_nodethat will contain theASTnode, to allow the user get more information
about the data structure node parent that is around the import.deffoo():frompkgimportmoduleY,moduleZ...In this case the absolute import is located inside a function namedfoo....# After introspect the fileabsolute_imports=imports_file.absolute_importsabsolute_imports[0].in_inner_scope->True# it's possible to get the ast node parent withabsolute_imports[0].outer_parent_node->ast.ASTobjectNotesThis library does not execute any part of the python  target code, this just make a static analysis over the code to describe the meta information about the imports in the file.LicenseThis project is licensed under the terms of the MIT license.