Resolve imports of a python file or module, with site packages & builtin modules excluded.
Project description
PHY-imports-resolver
Resolve imports of a python file or module recursively, exclude site packages & builtin modules.
This project is part of phy.
Install
If you intend to use this package as a library within your Python project, install it as follows:
pip install phy-imports-resolver
If you plan to use its command-line interface (CLI) to generate graphic diagram, install the cli
optional dependencies:
pip install phy-imports-resolver[cli]
How to use
Suppose the layout of your python project is:
src/
├──my_project/
│ ├──file1.py
│ ├──file2.py
│ ├──pkg1.py
│ │ ├── file3.py
│ │ └── __init__.py
│ └──__init__.py
└──script1.py
To resolve the imports tree:
-
Firstly you MUST specify a root directory named
project_dirto search within; in this case, it issrc. Ifproject_diris not explicitly provided, current directory is used. -
An entry file path is also required as the start point for the resolver to search from. If you want to resolve from a package instead a file, use the
__init__.pyfile of the package as the entry file.
In this case, if script1.py is the entry file to resolve, call the resolve(entry_file) method:
import phy_imports_resolver
from pathlib import Path
project_dir = Path('./src')
entry_file = project_dir / 'script1.py'
mod_imports_node = phy_imports_resolver.resolve(entry_file, project_dir)
The result mod_imports_node is a ModuleImportsNode object, which represents a file module or sub package in a hierarchical tree structure of importing relationship. See the definition of ModuleImportsNode:
class ModuleImportsNode:
def __init__(self, mod, project_dir, imports: List['ModuleImportsNode'], **kwargs):
...
where attribute mod stands for the file or package itself, and list attribute imports holds other modules imported to itself.
Recursively iterate the imports attributes of the entry ModuleImportsNode object will given the entire importing relationship tree.
The complete API docs can be found here.
use the CLI
Usage: phy-resolve-imports [OPTIONS] FILE
Resolve the imports of a python file or module, recursively.
FILE: path to the entry code file.
Options:
-f, --format [xml|png|svg] Specify the output format [default: xml]
-o, --output PATH Optional output file path.
--help Show this message and exit.
For the CLI, current work directory will be used as the root directory to search within.
API docs
These markdown docs are generated by pydoc-markdown:
pydoc-markdown -I src -m phy_imports_resolver --render-toc > docs/module.md
pydoc-markdown -I src -m phy_imports_resolver.resolver --render-toc > docs/resolver.md
pydoc-markdown -I src -m phy_imports_resolver.types --render-toc > docs/types.md
Reminder
This package DOES NOT support importlib to dynamically import modules, for the module imported by importlib can be
variable and thus can only be determined at runtime; otherwise, if the the module imported by importlib is
constant string, the developer can absolutely use import statement (non-top-placed) instead.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file phy_imports_resolver-0.1.2.tar.gz.
File metadata
- Download URL: phy_imports_resolver-0.1.2.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
474165813b5517755639f8b58a7ae6129867010f274733ba011522e738b9e5af
|
|
| MD5 |
188d8314def42c9efa56fbd6aae88edd
|
|
| BLAKE2b-256 |
2df2d15b7fbf920ee90cc8d692118b6328e5a2f7ee06451ba013c183b495b480
|
Provenance
The following attestation bundles were made for phy_imports_resolver-0.1.2.tar.gz:
Publisher:
python-publish.yml on phy-precompiler/imports-resolver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phy_imports_resolver-0.1.2.tar.gz -
Subject digest:
474165813b5517755639f8b58a7ae6129867010f274733ba011522e738b9e5af - Sigstore transparency entry: 712721678
- Sigstore integration time:
-
Permalink:
phy-precompiler/imports-resolver@9a30237cbaf5dfbb9771bbec3da3f28507f59c30 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/phy-precompiler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9a30237cbaf5dfbb9771bbec3da3f28507f59c30 -
Trigger Event:
release
-
Statement type:
File details
Details for the file phy_imports_resolver-0.1.2-py3-none-any.whl.
File metadata
- Download URL: phy_imports_resolver-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15bd1d791d768b3322b3764296665e04d03d524b4929474d32788934db8a2614
|
|
| MD5 |
bae01f66400375987652bbf8df4c4aae
|
|
| BLAKE2b-256 |
5a27563691a8df5cb5d341f564ede0d99f9f214697a1ecce56d5d10b12cfda4b
|
Provenance
The following attestation bundles were made for phy_imports_resolver-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on phy-precompiler/imports-resolver
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
phy_imports_resolver-0.1.2-py3-none-any.whl -
Subject digest:
15bd1d791d768b3322b3764296665e04d03d524b4929474d32788934db8a2614 - Sigstore transparency entry: 712721679
- Sigstore integration time:
-
Permalink:
phy-precompiler/imports-resolver@9a30237cbaf5dfbb9771bbec3da3f28507f59c30 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/phy-precompiler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@9a30237cbaf5dfbb9771bbec3da3f28507f59c30 -
Trigger Event:
release
-
Statement type: