# import_deps
[![PyPI version](https://img.shields.io/pypi/v/import-deps.svg)](https://pypi.org/project/import-deps/)
[![Python versions](https://img.shields.io/pypi/pyversions/import-deps.svg)](https://pypi.org/project/import-deps/)
[![CI Github actions](https://github.com/schettino72/import-deps/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/schettino72/import-deps/actions/workflows/test.yml?query=branch%3Amaster)
Find python module's import dependencies.
`import_deps` is based on [ast module](https://docs.python.org/3/library/ast.html) 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:
```python3
from . import foo_b
from .foo_c import obj_c
```
## Usage (CLI)
```bash
> import_deps foo/foo_a.py
foo.foo_b
foo.foo_c
```
## Usage (lib)
```python3
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.
```python3
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`
```python3
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`)
```python3
from import_deps import ast_imports
ast_imports('foo.py')
```
```python3
# 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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/schettino72/import-deps",
"name": "import-deps",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "import graph quality",
"author": "Eduardo Naufel Schettino",
"author_email": "schettino72@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/21/0c/487690243f9d792c7d1cfdcbc971e21b901140c367729ae83e17d08242ae/import_deps-0.3.0.tar.gz",
"platform": null,
"description": "# import_deps\n\n[![PyPI version](https://img.shields.io/pypi/v/import-deps.svg)](https://pypi.org/project/import-deps/)\n[![Python versions](https://img.shields.io/pypi/pyversions/import-deps.svg)](https://pypi.org/project/import-deps/)\n[![CI Github actions](https://github.com/schettino72/import-deps/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/schettino72/import-deps/actions/workflows/test.yml?query=branch%3Amaster)\n\nFind python module's import dependencies.\n\n`import_deps` is based on [ast module](https://docs.python.org/3/library/ast.html) from standard library,\nso the modules being analysed are *not* executed.\n\n\n## Install\n\n```\npip install import_deps\n```\n\n\n## Usage\n\n`import_deps` is designed to track only imports within a known set of package and modules.\n\nGiven a package with the modules:\n\n- `foo/__init__.py`\n- `foo/foo_a.py`\n- `foo/foo_b.py`\n- `foo/foo_c.py`\n\nWhere `foo_a.py` has the following imports:\n\n```python3\nfrom . import foo_b\nfrom .foo_c import obj_c\n```\n\n## Usage (CLI)\n\n```bash\n> import_deps foo/foo_a.py\nfoo.foo_b\nfoo.foo_c\n```\n\n\n## Usage (lib)\n\n```python3\nimport pathlib\nfrom import_deps import ModuleSet\n\n# First initialise a ModuleSet instance with a list str of modules to track\npkg_paths = pathlib.Path('foo').glob('**/*.py')\nmodule_set = ModuleSet([str(p) for p in pkg_paths])\n\n# then you can get the set of imports\nfor imported in module_set.mod_imports('foo.foo_a'):\n print(imported)\n\n# foo.foo_c\n# foo.foo_b\n```\n\n### ModuleSet\n\nYou can get a list of all modules in a `ModuleSet` by path or module's full qualified name.\n\n`by_path`\n\nNote that key for `by_path` must be exactly the as provided on ModuleSet initialization.\n\n```python3\nfor mod in sorted(module_set.by_path.keys()):\n print(mod)\n\n# results in:\n# foo/__init__.py\n# foo/foo_a.py\n# foo/foo_b.py\n# foo/foo_c.py\n```\n\n`by_name`\n\n```python3\nfor mod in sorted(module_set.by_name.keys()):\n print(mod)\n\n# results in:\n# foo.__init__\n# foo.foo_a\n# foo.foo_b\n# foo.foo_c\n```\n\n\n\n### ast_imports(file_path)\n\n`ast_imports` is a low level function that returns a list of entries for import statement in the module.\nThe parameter `file_path` can be a string or `pathlib.Path` instance.\n\nThe return value is a list of 4-tuple items with values:\n - module name (of the \"from\" statement, `None` if a plain `import`)\n - object name\n - as name\n - level of relative import (number of parent, `None` if plain `import`)\n\n\n```python3\nfrom import_deps import ast_imports\n\nast_imports('foo.py')\n```\n\n\n```python3\n# import datetime\n(None, 'datetime', None, None)\n\n# from datetime import time\n('datetime', 'time', None, 0)\n\n# from datetime import datetime as dt\n('datetime', 'datetime', 'dt', 0)\n\n# from .. import bar\n(None, 'bar', None, 2)\n\n# from .acme import baz\n('acme', 'baz', None, 1)\n\n\n# note that a single statement will contain one entry per imported \"name\"\n# from datetime import time, timedelta\n('datetime', 'time', None, 0)\n('datetime', 'timedelta', None, 0)\n```\n\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "find python module imports",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/schettino72/import-deps"
},
"split_keywords": [
"import",
"graph",
"quality"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "02ea8461a8fc157df724d56c7f0cce8d86af130bcb5e99622af2d3e6ceeca74f",
"md5": "9215c4c67cf58a79afad1ff72596db35",
"sha256": "4619659d57b8d5426177de7b98748a04446dc756c18544d8d75afda33bb0b008"
},
"downloads": -1,
"filename": "import_deps-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9215c4c67cf58a79afad1ff72596db35",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7604,
"upload_time": "2024-05-04T03:46:42",
"upload_time_iso_8601": "2024-05-04T03:46:42.369976Z",
"url": "https://files.pythonhosted.org/packages/02/ea/8461a8fc157df724d56c7f0cce8d86af130bcb5e99622af2d3e6ceeca74f/import_deps-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "210c487690243f9d792c7d1cfdcbc971e21b901140c367729ae83e17d08242ae",
"md5": "88014ad8fd04f08305797d70322ce7ae",
"sha256": "542544c69a435517dc8e556d0030044f49f2e61fff11ab8c2c612ac56ff62705"
},
"downloads": -1,
"filename": "import_deps-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "88014ad8fd04f08305797d70322ce7ae",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6624,
"upload_time": "2024-05-04T03:46:44",
"upload_time_iso_8601": "2024-05-04T03:46:44.193285Z",
"url": "https://files.pythonhosted.org/packages/21/0c/487690243f9d792c7d1cfdcbc971e21b901140c367729ae83e17d08242ae/import_deps-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-04 03:46:44",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "schettino72",
"github_project": "import-deps",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "import-deps"
}