Skip to main content

find python module imports

Project description

import_deps

PyPI version Python versions CI Github actions

Find python module's import dependencies.

import_deps is based on ast module from standard library, so the modules being analysed are not executed.

Install

pip install import_deps

Usage

import_deps is designed to track only imports within a known set of package and modules.

Given a package with the modules:

  • foo/__init__.py
  • foo/foo_a.py
  • foo/foo_b.py
  • foo/foo_c.py

Where foo_a.py has the following imports:

from . import foo_b
from .foo_c import obj_c
import pathlib
from import_deps import ModuleSet

# First initialise a ModuleSet instance with a list str of modules to track
pkg_paths = pathlib.Path('foo').glob('**/*.py')
module_set = ModuleSet([str(p) for p in pkg_paths])

# then you can get the set of imports
for imported in module_set.mod_imports('foo.foo_a'):
    print(imported)

# foo.foo_c
# foo.foo_b

ModuleSet

You can get a list of all modules in a ModuleSet by path or module's full qualified name.

by_path

Note that key for by_path must be exactly the as provided on ModuleSet initialization.

for mod in sorted(module_set.by_path.keys()):
    print(mod)

# results in:
# foo/__init__.py
# foo/foo_a.py
# foo/foo_b.py
# foo/foo_c.py

by_name

for mod in sorted(module_set.by_name.keys()):
    print(mod)

# results in:
# foo.__init__
# foo.foo_a
# foo.foo_b
# foo.foo_c

ast_imports(file_path)

ast_imports is a low level function that returns a list of entries for import statement in the module. The parameter file_path can be a string or pathlib.Path instance.

The return value is a list of 4-tuple items with values:

  • module name (of the "from" statement, None if a plain import)
  • object name
  • as name
  • level of relative import (number of parent, None if plain import)
from import_deps import ast_imports

ast_imports('foo.py')
# import datetime
(None, 'datetime', None, None)

# from datetime import time
('datetime', 'time', None, 0)

# from datetime import datetime as dt
('datetime', 'datetime', 'dt', 0)

# from .. import bar
(None, 'bar', None, 2)

# from .acme import baz
('acme', 'baz', None, 1)


# note that a single statement will contain one entry per imported "name"
# from datetime import time, timedelta
('datetime', 'time', None, 0)
('datetime', 'timedelta', None, 0)

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

import_deps-0.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

import_deps-0.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file import_deps-0.2.0.tar.gz.

File metadata

  • Download URL: import_deps-0.2.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for import_deps-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c63dbbc91cc6a6d9bd3ef3fd3193c285712a2d6bba2f3bc2582158d186a84fb1
MD5 1783461e687913fb9739a84f42b11a8e
BLAKE2b-256 61b367ecaed610e6f333a318482def6c5f0f1bd502e2a136a03bf3ebb58c6aa4

See more details on using hashes here.

File details

Details for the file import_deps-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: import_deps-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for import_deps-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 295d5f31cb35df7c471ec4b05bcde06c123b73d1f6d375894070d990b132572d
MD5 41aea408670d9855b20ed663ca41f409
BLAKE2b-256 72fb1bd7a8f3966667e82fb71aad81fcf9b2b4a7a74acb1fc5b0509d86212882

See more details on using hashes here.

Supported by

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