Skip to main content

Builds a queryable graph of the imports within one or more Python packages.

Project description

https://img.shields.io/pypi/v/grimp.svg Python versions CI Status

Builds a queryable graph of the imports within one or more Python packages.

  • Free software: BSD license

Quick start

Install grimp:

pip install grimp

Install the Python package you wish to analyse:

pip install somepackage

In Python, build the import graph for the package:

>>> import grimp
>>> graph = grimp.build_graph('somepackage')

You may now use the graph object to analyse the package. Some examples:

>>> graph.find_children('somepackage.foo')
{
    'somepackage.foo.one',
    'somepackage.foo.two',
}

>>> graph.find_descendants('somepackage.foo')
{
    'somepackage.foo.one',
    'somepackage.foo.two',
    'somepackage.foo.two.blue',
    'somepackage.foo.two.green',
}

>>> graph.find_modules_directly_imported_by('somepackage.foo')
{
    'somepackage.bar.one',
}

>>> graph.find_upstream_modules('somepackage.foo')
{
    'somepackage.bar.one',
    'somepackage.baz',
    'somepackage.foobar',
}

>>> graph.find_shortest_chain(importer='somepackage.foobar', imported='somepackage.foo')
(
    'somepackage.foobar',
    'somepackage.baz',
    'somepackage.foo',
)

>>> graph.get_import_details(importer='somepackage.foobar', imported='somepackage.baz'))
[
    {
        'importer': 'somepackage.foobar',
        'imported': 'somepackage.baz',
        'line_number': 5,
        'line_contents': 'from . import baz',
    },
]

External packages

By default, external dependencies will not be included. This can be overridden like so:

>>> graph = grimp.build_graph('somepackage', include_external_packages=True)
>>> graph.find_modules_directly_imported_by('somepackage.foo')
{
    'somepackage.bar.one',
    'os',
    'decimal',
    'sqlalchemy',
}

Multiple packages

You may analyse multiple root packages. To do this, pass each package name as a positional argument:

>>> graph = grimp.build_graph('somepackage', 'anotherpackage')
>>> graph.find_modules_directly_imported_by('somepackage.foo')
{
    'somepackage.bar.one',
    'anotherpackage.baz',
}

Namespace packages

Graphs can also be built from portions of namespace packages. To do this, provide the portion name, rather than the namespace name:

>>> graph = grimp.build_graph('somenamespace.foo')

What’s a namespace package?

Namespace packages are a Python feature allows subpackages to be distributed independently, while still importable under a shared namespace. This is, for example, used by the Python client for Google’s Cloud Logging API. When installed, it is importable in Python as google.cloud.logging. The parent packages google and google.cloud are both namespace packages, while google.cloud.logging is known as the ‘portion’. Other portions in the same namespace can be installed separately, for example google.cloud.secretmanager.

Grimp expects the package name passed to build_graph to be a portion, rather than a namespace package. So in the case of the example above, the graph should be built like so:

>>> graph = grimp.build_graph('google.cloud.logging')

If, instead, a namespace package is passed (e.g. grimp.build_graph('google.cloud')), Grimp will raise NamespacePackageEncountered.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grimp-3.0b1.tar.gz (832.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grimp-3.0b1-cp310-cp310-macosx_10_7_x86_64.whl (351.5 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

File details

Details for the file grimp-3.0b1.tar.gz.

File metadata

  • Download URL: grimp-3.0b1.tar.gz
  • Upload date:
  • Size: 832.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for grimp-3.0b1.tar.gz
Algorithm Hash digest
SHA256 c1bc211549092508b3a9b1a2ffbde533a74985740fe260589c847727c3eedad7
MD5 82d55e20efe358f34a7c5df3466f4851
BLAKE2b-256 8ecfb69e1b98325501d080b4cd57915e07317fc41b3f25a24509e5eca75a2dcc

See more details on using hashes here.

File details

Details for the file grimp-3.0b1-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for grimp-3.0b1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a18a93578b4211d1f2d3313ae755702fa34685efbfdcaecc6ad8cc9db5f16c47
MD5 4cde03cd85d01053ad940587afaf9d8f
BLAKE2b-256 f3a6c0717fea1122287c9e89269f84877a86c1011b5bfb3a656f4b8a5be5bcb6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page