https://github.com/ultralytics/autoimport
Effortless lazy Python imports
https://github.com/ultralytics/autoimport
best-practices code imports lazy lazy-loading lazyload python tools ultralytics yolo
Last synced: 6 months ago
JSON representation
Effortless lazy Python imports
- Host: GitHub
- URL: https://github.com/ultralytics/autoimport
- Owner: ultralytics
- License: agpl-3.0
- Created: 2025-01-16T13:49:58.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-11T00:22:18.000Z (9 months ago)
- Last Synced: 2025-07-18T23:44:17.865Z (6 months ago)
- Topics: best-practices, code, imports, lazy, lazy-loading, lazyload, python, tools, ultralytics, yolo
- Language: Python
- Homepage: https://ultralytics.com
- Size: 49.8 KB
- Stars: 4
- Watchers: 0
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# โก๏ธ `autoimport`: Effortless Lazy Imports in Python
`autoimport` is a lightweight Python package providing effortless **lazy imports**. By using the `lazy` context manager, modules are imported only when they are actually accessed, improving application startup times and reducing the initial memory footprint. This is ideal for projects with heavy dependencies that are not always needed immediately. The `ultralytics-autoimport` package is published on [PyPI](https://pypi.org/project/ultralytics-autoimport/) for easy installation.
[](https://github.com/ultralytics/autoimport/actions/workflows/ci.yml)
[](https://github.com/ultralytics/autoimport/actions/workflows/format.yml)
[](https://discord.com/invite/ultralytics)
[](https://community.ultralytics.com/)
[](https://reddit.com/r/ultralytics)
## ๐ Quick Start
Install the `ultralytics-autoimport` package from [PyPI](https://pypi.org/project/ultralytics-autoimport/):
[](https://pypi.org/project/ultralytics-autoimport/)
[](https://www.pepy.tech/projects/ultralytics-autoimport)
[](https://pypi.org/project/ultralytics-autoimport/)
```bash
pip install ultralytics-autoimport
```
Use the `lazy` context manager to defer imports according to standard [Python import mechanics](https://docs.python.org/3/reference/import.html):
```python
import time
from autoimport import lazy
with lazy():
t0 = time.perf_counter()
import torch # Import is deferred until first use
print(f"Initial import time: {time.perf_counter() - t0:.3f}s") # Example output: 0.000s
t1 = time.perf_counter()
# The package is actually loaded here when torch.cuda is accessed
torch.cuda.is_available()
print(f"First use time: {time.perf_counter() - t1:.3f}s") # Example output: 0.462s
```
In this example, the `import torch` statement inside the `lazy()` context doesn't immediately load the [PyTorch](https://pytorch.org/) library. The actual import happens only when `torch.cuda.is_available()` is called, demonstrating the deferred loading mechanism.
## ๐ Repository Structure
The repository is organized for clarity and ease of development:
- `autoimport/`: Contains the source code of the `autoimport` package.
- `tests/`: Unit tests to ensure code reliability using frameworks like [pytest](https://docs.pytest.org/en/stable/).
- `pyproject.toml`: Project configuration following [PEP 621](https://peps.python.org/pep-0621/), including dependencies and packaging details.
- `.gitignore`: Specifies files intentionally untracked by Git.
- `LICENSE`: The open-source license for the project ([AGPL-3.0](https://opensource.org/license/agpl-v3)).
- `.github/workflows/`: [GitHub Actions](https://docs.github.com/en/actions) workflows for Continuous Integration (CI) and Continuous Deployment (CD).
```
autoimport/
โ
โโโ autoimport/ # Package source code
โ โโโ __init__.py
โ โโโ ...
โ
โโโ tests/ # Unit tests
โ โโโ __init__.py
โ โโโ test_autoimport.py
โ โโโ ...
โ
โโโ pyproject.toml # Project configuration
โโโ .gitignore # Git ignore rules
โโโ LICENSE # AGPL-3.0 License file
โโโ README.md # This file
โโโ .github/workflows/ # GitHub Actions CI/CD workflows
โโโ ci.yml
โโโ format.yml
```
### ๐ Source Code in `autoimport/` Directory
The `autoimport/` directory contains the core Python code for the `autoimport` package.
### ๐งช Testing with the `tests/` Directory
The `tests/` directory includes tests designed to maintain code quality and prevent regressions during development.
## โ Starting a New Project
This repository can also serve as a template for new Python projects at [Ultralytics](https://www.ultralytics.com/).
To use it as a template:
1. **Create a New Repository:** Use this repository as a template to generate a new one for your project.
2. **Customize:** Modify `pyproject.toml`, `.pre-commit-config.yaml` (if using [pre-commit](https://pre-commit.com/)), and GitHub workflow YAML files as needed for your specific project requirements.
3. **Develop:** Add your source code to a new directory (e.g., `your_package_name/`) and corresponding tests to the `tests/` directory.
4. **Document:** Update the README.md file and add any necessary documentation within the [Ultralytics Docs](https://docs.ultralytics.com/).
5. **CI/CD:** Utilize the pre-configured [GitHub Actions](https://docs.github.com/en/actions) for automated testing and formatting checks.
## ๐ก Contribute
Ultralytics thrives on community contributions! We appreciate any help, from reporting bugs to submitting pull requests. Please see our [Contributing Guide](https://docs.ultralytics.com/help/contributing/) for more details on how to get involved. You can also share your feedback through our quick [Survey](https://www.ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey). Thank you ๐ to all our contributors!
[](https://github.com/ultralytics/autoimport/graphs/contributors)
## ๐ License
Ultralytics provides two licensing options to accommodate different use cases:
- **AGPL-3.0 License**: This [OSI-approved](https://opensource.org/license) open-source license is ideal for students, enthusiasts, and researchers who wish to share their work openly. See the [LICENSE](https://github.com/ultralytics/autoimport/blob/main/LICENSE) file for full details.
- **Enterprise License**: Designed for commercial use, this license allows for the integration of Ultralytics software and AI models into commercial products and services without the open-source requirements of AGPL-3.0. Please contact [Ultralytics Licensing](https://www.ultralytics.com/license) for more information.
## ๐ฎ Contact
For bug reports, feature requests, or questions, please open a [GitHub Issue](https://github.com/ultralytics/autoimport/issues). For community support and discussions, join our [Discord](https://discord.com/invite/ultralytics) server!






