An utility tool to check for inappropriate imports between modules in a Python project. and can be use to enforce modular boundaries.
Project description
Modular Monolith Import Checker
Overview
The Modular Monolith Import Checker is a Python utility designed to enforce modular boundaries within your codebase. It ensures that modules in a modular monolith architecture do not import each other inappropriately, maintaining clear module boundaries and a clean architecture. It can also be used to enforce modular boundaries in a monorepo and ensuring modular boundaries are enforced both locally and in CI/CD pipelines.
Features
- Detect Forbidden Imports: Identifies cases where a module imports another module that it shouldn't.
- Flexible Path Handling: Supports both absolute and relative paths for specifying base directories.
- Dynamic Module List: Allows you to specify a list of modules to check.
Installation
To use the Modular Monolith Import Checker, you need Python 3 installed. Install the package using pip:
pip install module-import-checker
Usage
You can run the import checker from the command line or import it into your Python scripts.
Command-Line Interface
Run the import checker directly from the command line with various configurations:
-
Modules at the Project Root:
import_module_checker /path/to/project module1 module2 module3
/path/to/project
is the base directory of your project.module1
,module2
,module3
, etc., are the names of the modules you want to check.
-
Modules in the Current Directory:
import_module_checker . module1 module2 module3
- Use
.
if you are running the script from the project root directory.
- Use
-
Modules Inside a Subdirectory:
import_module_checker project_name module1 module2 module3
project_name
is the name of the subdirectory containing your modules.
Example
To check imports in a project located in /home/user/myproject
where modules are named auth
, users
, and payments
, run:
import_module_checker /home/user/myproject auth users payments
You can create a bash script to run this command automatically:
#!/bin/bash
import_module_checker /home/user/myproject auth users payments
Command-Line Help
For detailed usage information, run:
import_module_checker --help
Using within a Build Process
To integrate the import checker into your CI/CD pipeline and ensure that builds fail if there are forbidden imports, you can include it in your build script. This will either pass and continue the build process or fail and exit. For example, in a GitHub Actions workflow:
name: CI
on: [push, pull_request]
jobs:
import-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install --no-cache-dir module-import-checker
- name: Run Import Checker
run: |
import_module_checker project_name module1 module2 module3
Example Output
Checking imports in Base Directory: /home/user/myproject
Checking module: /home/user/myproject/auth/
Import check failed with errors:
Forbidden import detected: 'users' imports 'auth' in /home/user/myproject/users/services.py
Contributing
If you would like to contribute to this project, please fork the repository and submit a pull request. Ensure that your code follows the existing style and includes tests for new features.
License
This project is licensed under the GNU General Public License - see the LICENSE file for details.
Contact
For any questions or issues, please contact abiolaadeshinaadedayo@gmail.com.
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
Built Distribution
File details
Details for the file module-import-checker-0.0.13.tar.gz
.
File metadata
- Download URL: module-import-checker-0.0.13.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2471694d9882f3058b1d37303ce65c406268e665e8df6116445f742282057b3e |
|
MD5 | 7abf08b9e6a55363e0eba210ef8ba078 |
|
BLAKE2b-256 | c1a1618fe37385c9538084866d52bb51c2392db2cbac302948c8e633547e106f |
File details
Details for the file module_import_checker-0.0.13-py3-none-any.whl
.
File metadata
- Download URL: module_import_checker-0.0.13-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d21be3c4e4afbb88acff8d10cea3b613041260a1cad21415712e32bfdb2aa8f |
|
MD5 | afcc5f7f5523bfb09895fc5bdc924c46 |
|
BLAKE2b-256 | d162dec2da5ca97de5839b5036997c4346b496db5e675597d669e461fba96ffb |