diff --git a/examples/custom_flow_script.py b/examples/custom_flow_script.py index 3165ad7b5..0b1f91373 100644 --- a/examples/custom_flow_script.py +++ b/examples/custom_flow_script.py @@ -4,10 +4,15 @@ Example script showing how to run a CrewAI flow from a custom script. This example demonstrates how to avoid the ModuleNotFoundError when starting flows from custom scripts outside of the CLI command context. """ +import logging import os from crewai.utilities.path_utils import add_project_to_path +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +logger.info("Adding project directory to Python path") add_project_to_path() from my_flow.main import MyFlow # noqa: E402 diff --git a/src/crewai/cli/kickoff_flow.py b/src/crewai/cli/kickoff_flow.py index 08c359f32..962090805 100644 --- a/src/crewai/cli/kickoff_flow.py +++ b/src/crewai/cli/kickoff_flow.py @@ -9,7 +9,11 @@ def kickoff_flow() -> None: """ Kickoff the flow by running a command in the UV environment. """ - add_project_to_path() + try: + add_project_to_path() + except ValueError as e: + click.echo(f"Error setting up project path: {e}", err=True) + return command = ["uv", "run", "kickoff"] diff --git a/src/crewai/utilities/path_utils.py b/src/crewai/utilities/path_utils.py index 22d9a24ee..13d639b9d 100644 --- a/src/crewai/utilities/path_utils.py +++ b/src/crewai/utilities/path_utils.py @@ -1,9 +1,13 @@ +import logging import os import sys from pathlib import Path from typing import Optional +logger = logging.getLogger(__name__) + + def add_project_to_path(project_dir: Optional[str] = None) -> None: """ Add the project directory to the Python path to resolve module imports. @@ -15,6 +19,9 @@ def add_project_to_path(project_dir: Optional[str] = None) -> None: project_dir: Optional path to the project directory. If not provided, the current working directory is used. + Raises: + ValueError: If the provided directory does not exist. + Example: ```python from crewai.utilities.path_utils import add_project_to_path @@ -29,15 +36,26 @@ def add_project_to_path(project_dir: Optional[str] = None) -> None: """ if project_dir is None: project_dir = os.getcwd() + logger.debug(f"Using current working directory: {project_dir}") + else: + logger.debug(f"Using provided directory: {project_dir}") project_path = Path(project_dir).resolve() + if not project_path.exists(): + raise ValueError(f"Invalid directory: {project_dir} does not exist") + if (project_path / "src").exists() and (project_path / "src").is_dir(): + logger.debug(f"Found 'src' directory in {project_path}") if str(project_path) not in sys.path: sys.path.insert(0, str(project_path)) + logger.debug(f"Added {project_path} to sys.path") if str(project_path / "src") not in sys.path: sys.path.insert(0, str(project_path / "src")) + logger.debug(f"Added {project_path / 'src'} to sys.path") else: + logger.debug(f"No 'src' directory found in {project_path}") if str(project_path) not in sys.path: sys.path.insert(0, str(project_path)) + logger.debug(f"Added {project_path} to sys.path")