Grimp provides an API in the form of an ImportGraph
that represents all the internal imports within a top-level Python package. This object has various methods that make it easy to find out information about that package's structure and interdependencies.
The terminology around Python packages and modules can be a little confusing. Here are the definitions we use, taken in part from the official Python docs:
- Module: A file containing Python definitions and statements. This includes ordinary
.py
files and__init__.py
files. - Package: A special kind of module that namespaces other modules using dotted module names. For example, the module name
A.B
designates a submodule namedB
in a package namedA
. Packages take the form of__init__.py
files in a container directory. Packages may contain other packages. A package is also a module. - Top Level Package: A package in the root namespace - in other words, one that is not a subpackage. For example,
A
is a top level package, butA.B
is not. - Graph: A graph in the mathematical sense of a collection of items with relationships between them. Grimp's
ImportGraph
is a directed graph of all the internal imports contained in a particular top level package. - Direct Import: An import from one module to another.
- Import Path: A chain of imports between two modules, possibly via other modules. For example, if
mypackage.foo
importsmypackage.bar
, which in turn importsmypackage.baz
, then there is an import path betweenmypackage.foo
andmypackage.baz
.
import grimp
graph = grimp.build_graph('name_)
Add a direct import between two modules to the graph. If the modules are not already present, they will be added to the graph.
- param str importer
The name of the module that is importing the other module.
- param str imported
The name of the module being imported.
- param int line_number
The line number of the import statement in the module.
- param str line_contents
The line that contains the import statement.
- return
None