Skip to content

[BUG][CLI does not find env] #1541

@kyegomez

Description

@kyegomez

Description

The Swarms CLI does not reliably locate or load the .env file, causing commands to fail silently when API keys and environment variables are not picked up.

Steps to Reproduce

  1. Create a .env file in the project root with valid API keys (e.g. OPENAI_API_KEY=sk-...)
  2. Run any CLI command: swarms agent --task "hello"
  3. The CLI proceeds as if no API keys are set, or raises an API key error

Expected Behavior

The CLI should automatically discover and load .env from the current working directory (or the user's home directory as a fallback) before executing any command.

Actual Behavior

API keys defined in .env are not loaded. Commands either fail with an API key error or the setup-check command reports no active provider.

Environment

  • OS: macOS / Linux
  • Python version: 3.10+
  • Swarms version: swarms --version

Root Cause (suspected)

load_swarms_env() is called at module import time in swarms/cli/main.py, but if the working directory at import time differs from where the .env lives, the file is not found. Additionally, python-dotenv is not explicitly searching parent directories.

Suggested Fix

  • Use find_dotenv() from python-dotenv to search parent directories
  • Or explicitly resolve the .env path relative to Path.cwd() and fall back to Path.home()

Additional Context

Running swarms setup-check should display a clear error when .env is missing or empty rather than silently continuing with no keys loaded.

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions