autoimport
is a lightweight Python package that provides effortless lazy imports. By using the lazy
context manager, modules are imported only when they are actually accessed, improving startup times and reducing initial memory footprint. Ideal for projects with heavy dependencies that are not always needed. The ultralytics-autoimport
package is published on PyPI for easy installation.
Install the ultralytics-autoimport
package from PyPI:
pip install ultralytics-autoimport
Use the lazy
context manager to defer imports:
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()
torch.cuda.is_available() # Package is actually loaded here
print(f"First use time: {time.perf_counter() - t1:.3f}s") # Example output: 0.462s
The repository is organized for clarity and ease of development:
autoimport/
: Contains the source code of theautoimport
package.tests/
: Unit tests to ensure code reliability.pyproject.toml
: Project configuration, including dependencies and packaging details..gitignore
: Specifies files to be excluded from Git tracking.LICENSE
: The open-source license for the project (AGPL-3.0)..github/workflows/
: GitHub Actions workflows for CI/CD.
autoimport/
│
├── autoimport/
│ ├── __init__.py
│ └── ...
│
├── tests/
│ ├── __init__.py
│ ├── test_autoimport.py
│ └── ...
│
├── pyproject.toml
└── README.md
The src/autoimport/
directory contains the core Python code for the autoimport
package.
The tests/
directory includes tests to maintain code quality and prevent regressions.
This repository can also serve as a template for new Python projects at Ultralytics.
To use it as a template:
- Create a New Repository: Use this as a template to generate a new repository.
- Customize: Modify
pyproject.toml
,.pre-commit-config.yaml
(if applicable), and GitHub workflow YAMLs as needed. - Develop: Add your code to
src/your_package_name/
and tests totests/
. - Document: Update the README and add documentation if necessary.
- CI/CD: Utilize the pre-configured GitHub Actions for automated testing.
Ultralytics thrives on community contributions! Please see our Contributing Guide for details on how to participate. Share your feedback in our Survey. Thank you 🙏 to all our contributors!
Ultralytics offers two licensing options:
- AGPL-3.0 License: An OSI-approved open-source license for students, enthusiasts, and researchers. See LICENSE for details.
- Enterprise License: For commercial use, allowing integration of Ultralytics software and AI models into commercial products without the AGPL-3.0 copyleft restrictions. Contact Ultralytics Licensing if this is required.
For issues or feature suggestions, please open a GitHub Issue. You can also join our Discord community!