Skip to content

Commit

Permalink
Merge pull request ag2ai#362 from ag2ai/353-bug-modulenotfounderror-j…
Browse files Browse the repository at this point in the history
…upyter-kernel-gateway

Fix Jupyter Kernel Gateway not found at import
  • Loading branch information
qingyun-wu authored Jan 6, 2025
2 parents a3bfe23 + d3f6721 commit a8b1bbb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ test/test_files/agenteval-in-out/out/

# local cache or coding foler
local_cache/
coding/
# coding/

# Files created by tests
*tmp_code_*
Expand Down
8 changes: 8 additions & 0 deletions autogen/coding/jupyter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
#
# Original portions of this file are derived from https://github.com/microsoft/autogen under the MIT License.
# SPDX-License-Identifier: MIT
from .helpers import is_jupyter_kernel_gateway_installed

if not is_jupyter_kernel_gateway_installed():
raise ImportError(
"jupyter-kernel-gateway is required for JupyterCodeExecutor, please install it with `pip install ag2[jupyter-executor]`"
)


from .base import JupyterConnectable, JupyterConnectionInfo
from .docker_jupyter_server import DockerJupyterServer
from .embedded_ipython_code_executor import EmbeddedIPythonCodeExecutor
Expand Down
5 changes: 1 addition & 4 deletions autogen/coding/jupyter/embedded_ipython_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@
import uuid
from pathlib import Path
from queue import Empty
from typing import Any, List
from typing import Any

# this is needed for CI to work. The import of this file should fail if jupyter-kernel-gateway is not installed
import jupyter_kernel_gateway
from jupyter_client import KernelManager # type: ignore[attr-defined]
from jupyter_client.kernelspec import KernelSpecManager
from pydantic import BaseModel, Field, field_validator

from ...agentchat.agent import LLMAgent
from ..base import CodeBlock, CodeExtractor, IPythonCodeResult
from ..markdown_code_extractor import MarkdownCodeExtractor

Expand Down
26 changes: 26 additions & 0 deletions autogen/coding/jupyter/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
#
# SPDX-License-Identifier: Apache-2.0

import subprocess
from logging import getLogger

logger = getLogger(__name__)

__all__ = ["is_jupyter_kernel_gateway_installed"]


def is_jupyter_kernel_gateway_installed() -> bool:
try:
subprocess.run(
["jupyter", "kernelgateway", "--version"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
return True
except (subprocess.CalledProcessError, FileNotFoundError):
logger.warning(
"jupyter-kernel-gateway is required for JupyterCodeExecutor, please install it with `pip install ag2[jupyter-executor]`"
)
return False
Empty file added test/coding/__init__.py
Empty file.
6 changes: 3 additions & 3 deletions test/coding/test_embedded_ipython_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import tempfile
import uuid
from pathlib import Path
from typing import Dict, Type, Union
from typing import Union

import pytest
from conftest import MOCK_OPEN_AI_API_KEY, skip_docker, skip_openai # noqa: E402

from autogen.agentchat.conversable_agent import ConversableAgent
from autogen.coding.base import CodeBlock, CodeExecutor
from autogen.coding.factory import CodeExecutorFactory
from autogen.oai.openai_utils import config_list_from_json

from ..conftest import MOCK_OPEN_AI_API_KEY, skip_docker # noqa: E402

try:
from autogen.coding.jupyter import (
Expand Down

0 comments on commit a8b1bbb

Please sign in to comment.