An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

Ultralytics logo

# โšก๏ธ `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.

[![autoimport CI](https://github.com/ultralytics/autoimport/actions/workflows/ci.yml/badge.svg)](https://github.com/ultralytics/autoimport/actions/workflows/ci.yml)
[![Ultralytics Actions](https://github.com/ultralytics/autoimport/actions/workflows/format.yml/badge.svg)](https://github.com/ultralytics/autoimport/actions/workflows/format.yml)
[![Ultralytics Discord](https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue)](https://discord.com/invite/ultralytics)
[![Ultralytics Forums](https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue)](https://community.ultralytics.com/)
[![Ultralytics Reddit](https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue)](https://reddit.com/r/ultralytics)

## ๐Ÿš€ Quick Start

Install the `ultralytics-autoimport` package from [PyPI](https://pypi.org/project/ultralytics-autoimport/):

[![PyPI - Version](https://img.shields.io/pypi/v/ultralytics-autoimport.svg)](https://pypi.org/project/ultralytics-autoimport/)
[![Downloads](https://static.pepy.tech/badge/ultralytics-autoimport)](https://www.pepy.tech/projects/ultralytics-autoimport)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ultralytics-autoimport.svg)](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!

[![Ultralytics open-source contributors](https://raw.githubusercontent.com/ultralytics/assets/main/im/image-contributors.png)](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!




Ultralytics GitHub
space
Ultralytics LinkedIn
space
Ultralytics Twitter
space
Ultralytics YouTube
space
Ultralytics TikTok
space
Ultralytics BiliBili
space
Ultralytics Discord