# optional_import_block

## ``autogen.import_utils.optional_import_block``

```python
optional_import_block()
```

Guard a block of code to suppress ImportErrors

A context manager to temporarily suppress ImportErrors. Use this to attempt imports without failing immediately on missing modules.

### Example:

```python
with optional_import_block():
    import some_module
    import some_other_module
```

### Source code in `autogen/import_utils.py`

|     |     |
| --- | --- |
| ```<br>165<br>166<br>167<br>168<br>169<br>170<br>171<br>172<br>173<br>174<br>175<br>176<br>177<br>178<br>179<br>180<br>181<br>182<br>183<br>184<br>185<br>186<br>``` | ````<br>@contextmanager<br>def optional_import_block() -> Generator[Result, None, None]:<br>    """Guard a block of code to suppress ImportErrors<br>    A context manager to temporarily suppress ImportErrors.<br>    Use this to attempt imports without failing immediately on missing modules.<br>    Example:<br>    ```python<br>    with optional_import_block():<br>        import some_module<br>        import some_other_module<br>    ```<br>    """<br>    result = Result()<br>    try:<br>        yield result<br>        result._failed = False<br>    except ImportError as e:<br>        # Ignore ImportErrors during this context<br>        logger.debug(f"Ignoring ImportError: {e}")<br>        result._failed = True<br>```` |
