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 for easy installation.
Install the ultralytics-autoimport package from PyPI:
pip install ultralytics-autoimportUse the lazy context manager to defer imports according to standard Python import mechanics:
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.462sIn this example, the import torch statement inside the lazy() context doesn't immediately load the PyTorch library. The actual import happens only when torch.cuda.is_available() is called, demonstrating the deferred loading mechanism.
The repository is organized for clarity and ease of development:
autoimport/: Contains the source code of theautoimportpackage.tests/: Unit tests to ensure code reliability using frameworks like pytest.pyproject.toml: Project configuration following PEP 621, including dependencies and packaging details..gitignore: Specifies files intentionally untracked by Git.LICENSE: The open-source license for the project (AGPL-3.0)..github/workflows/: GitHub 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
The autoimport/ directory contains the core Python code for the autoimport package.
The tests/ directory includes tests designed to maintain code quality and prevent regressions during development.
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 repository as a template to generate a new one for your project.
- Customize: Modify
pyproject.toml,.pre-commit-config.yaml(if using pre-commit), and GitHub workflow YAML files as needed for your specific project requirements. - Develop: Add your source code to a new directory (e.g.,
your_package_name/) and corresponding tests to thetests/directory. - Document: Update the README.md file and add any necessary documentation within the Ultralytics Docs.
- CI/CD: Utilize the pre-configured GitHub Actions for automated testing and formatting checks.
Ultralytics thrives on community contributions! We appreciate any help, from reporting bugs to submitting pull requests. Please see our Contributing Guide for more details on how to get involved. You can also share your feedback through our quick Survey. Thank you 🙏 to all our contributors!
Ultralytics provides two licensing options to accommodate different use cases:
- AGPL-3.0 License: This OSI-approved open-source license is ideal for students, enthusiasts, and researchers who wish to share their work openly. See the 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 for more information.
For bug reports, feature requests, or questions, please open a GitHub Issue. For community support and discussions, join our Discord server!








