# optional_import_block

## `autogen.import_utils.optional_import_block [#](https://docs.ag2.ai/0.8.0/docs/api-reference/autogen/import_utils/optional_import_block/#autogen.import_utils.optional_import_block "Permanent link")`

```
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:

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

Source code in `autogen/import_utils.py`

|     |     |
| --- | --- |
| ```<br>35<br>36<br>37<br>38<br>39<br>40<br>41<br>42<br>43<br>44<br>45<br>46<br>47<br>48<br>49<br>50<br>51<br>52<br>53<br>54<br>55<br>56<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>```` |
