Skip to main content

Auto import

Project description

Auto Import Py

A Python library for automatically importing modules from a directory structure.

English | 한국어

Features

  • 📁 Automatically imports all Python modules from a directory structure
  • 🔄 Recursively traverses subdirectories
  • ⚙️ Returns a list of imported module objects
  • 🚀 Simple and lightweight implementation
  • 🛣️ Works with any directory structure

Installation

pip install auto-import-py
uv add auto-import-py

Usage

Basic Usage

from auto_import import auto_import
from pathlib import Path

# Import all modules from the current directory
modules = auto_import()

# Import all modules from a specific directory
modules = auto_import("my_modules")

# Import all modules from a Path object
modules = auto_import(Path("src/components"))

Directory Structure Example

my_modules/
├── users.py          # Will be imported
├── items.py          # Will be imported
├── __init__.py       # Will be filtered out
└── v1/
    ├── admin.py      # Will be imported
    └── __init__.py   # Will be filtered out

Module File Example

# my_modules/users.py
def get_users():
    return {"users": []}

def create_user(name: str):
    return {"name": name, "id": 1}

# my_modules/items.py
class Item:
    def __init__(self, name: str):
        self.name = name

def get_items():
    return [Item("item1"), Item("item2")]

Using Imported Modules

from auto_import import auto_import

# Import all modules
modules = auto_import("my_modules")

# Access functions and classes from imported modules
for module in modules:
    print(f"Module: {module.__name__}")
    
    # Get all functions from the module
    functions = [attr for attr in dir(module) if callable(getattr(module, attr)) and not attr.startswith('_')]
    print(f"Functions: {functions}")
    
    # Get all classes from the module
    classes = [attr for attr in dir(module) if isinstance(getattr(module, attr), type) and not attr.startswith('_')]
    print(f"Classes: {classes}")

Advanced Usage

from auto_import import auto_import
import inspect

# Import modules and inspect their contents
modules = auto_import("src")

for module in modules:
    print(f"\n=== {module.__name__} ===")
    
    # Get all callable objects
    for name, obj in inspect.getmembers(module, callable):
        if not name.startswith('_'):
            print(f"Callable: {name}")
    
    # Get all classes
    for name, obj in inspect.getmembers(module, inspect.isclass):
        if not name.startswith('_'):
            print(f"Class: {name}")

API Reference

auto_import(dir_path: Path | str = ".") -> list[ModuleType]

Automatically imports all Python modules from the specified directory.

Parameters:

  • dir_path (Path | str): Directory path to import modules from (default: ".")

Returns:

  • list[ModuleType]: List of imported module objects

Behavior:

  1. Recursively traverses the specified directory
  2. Finds all .py files
  3. Imports each module using importlib.import_module
  4. Returns a list of successfully imported modules

Note: The function will return an empty list if the directory doesn't exist.

Development

Install Dependencies

# Install development dependencies
uv sync

Run Tests

# Run all tests
uv run pytest

Code Quality Checks

# Linting
ruff check src/ tests/

# Formatting
ruff format src/ tests/

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Contributing

Bug reports, feature requests, and pull requests are welcome! Please create an issue first before contributing.

Author

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_import_py-0.1.0.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_import_py-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file auto_import_py-0.1.0.tar.gz.

File metadata

  • Download URL: auto_import_py-0.1.0.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for auto_import_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 012555e05fd9ca6ce35c9b7ac7fe800cf1e3d5dea823f4a40a4fd118b88cd5d6
MD5 f76899b1a1bc3d1a5a3795a7b47e1a6a
BLAKE2b-256 313c3bb984d49109cdf025aa21979b1cd916aff2014c773fd7118892f6f94935

See more details on using hashes here.

File details

Details for the file auto_import_py-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for auto_import_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb14e4c93e28465ca8b164ea53b3a9239b3c59903eaf9a018b37f6356dd6e1f9
MD5 4daa17462c39f23390096e9902282f53
BLAKE2b-256 d867acac63933833a60fbdbe382fd24c99390e9e616cc0b58d3b74e98d49e1ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page