modulegraph / doc / modulegraph.rst

:mod:`modulegraph.modulegraph` --- Find modules used by a script

This module defines :class:`ModuleGraph`, which is used to find the dependencies of scripts using bytecode analysis.

A number of APIs in this module refer to filesystem path. Those paths can refer to files inside zipfiles (for example when there are zipped egg files on :data:`sys.path`). Filenames referring to entries in a zipfile are not marked any way, if "" refers to a zipfile, that is "" will be used to refer to embedded/file inside the zipfile.

The actual graph

Create a new ModuleGraph object. Use the :meth:`run_script` method to add scripts, and their dependencies to the graph.

param path:Python search path to use, defaults to :data:`sys.path`
param excludes:Iterable with module names that should not be included as a dependency
param replace_paths:
 List of pathname rewrites (old, new). When this argument is supplied the co_filename attributes of code objects get rewritten before scanning them for dependencies.
param implies:Implied module dependencies, a mapping from a module name to the list of modules it depends on. Use this to tell modulegraph about dependencies that cannot be found by code inspection (such as imports from C code or using the :func:`__import__` function).
param graph:A precreated :class:`Graph <altgraph.Graph.Graph>` object to use, the default is to create a new one.
param debug:The :class:`ObjectGraph <altgraph.ObjectGraph.ObjectGraph>` debug level.

Mostly internal methods

The methods in this section should be considered as methods for subclassing at best, please let us know if you need these methods in your code as they are on track to be made private methods before the 1.0 release.


The methods in this section will be refactored in a future release, the current architecture makes it unnecessarily hard to write proper tests.

Graph nodes

The :class:`ModuleGraph` contains nodes that represent the various types of modules.

This is a subclass of string that is used to mark module aliases.

Base class for nodes, which provides the common functionality.

Nodes can by used as mappings for storing arbitrary data in the node.

Nodes are compared by comparing their identifier.

A node that represents an alias from a name to another node.

The value of attribute graphident for this node will be the value of name, the other :class:`Node` attributed are references to those attributed in node.

Base class for nodes that should be ignored for some reason

A module that is explicitly excluded.

A module that is imported but cannot be located.

A python script.

The base class for actual modules. The name is the possibly dotted module name, filename is the filesystem path to the module and path is the value of __path__ for the module.

A built-in module (on in :data:`sys.builtin_module_names`).

A module for which the python source code is available.

A module for which only byte-code is available.

Represents a python package

A native extension


A number of other node types are defined in the module. Those modules aren't used by modulegraph and will be removed in a future version.

Utility functions