Skip to content

ultralytics/autoimport

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 for easy installation.

autoimport CI Ultralytics Actions Ultralytics Discord Ultralytics Forums Ultralytics Reddit

🚀 Quick Start

Install the ultralytics-autoimport package from PyPI:

PyPI - Version Downloads PyPI - Python Version

pip install ultralytics-autoimport

Use 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.462s

In 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.

🗂 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.
  • 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

📂 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.

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), 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.
  5. CI/CD: Utilize the pre-configured GitHub 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 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 open-source contributors

📄 License

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.

📮 Contact

For bug reports, feature requests, or questions, please open a GitHub Issue. For community support and discussions, join our Discord server!


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