.env File Loading

Where PraisonAI looks for your .env file and how precedence works

PraisonAI auto-loads a .env from your current working directory before running any CLI command.

Diagram

Yes

No, walk up

Yes

No

📁 Current directory

🔍 .env found?

⚙️ load into env

🔍 parent has .env?

🚫 Nothing loaded

✅ Agent sees keys

Quick Start

  1. Simple Usage

    Create a .env next to where you run PraisonAI:

    bash
    # .env
    OPENAI_API_KEY=sk-...

    Run an agent file from that directory:

    python
    from praisonaiagents import Agent
    
    agent = Agent(name="Helper", instructions="You are a helpful assistant")
    agent.start("hello")
    bash
    praisonai run "hello"
  2. With Configuration

    praisonai.api.call does not read .env on import. Opt in with an environment variable:

    bash
    export PRAISONAI_CALL_LOAD_DOTENV=true
    python -c "import praisonai.api.call"

    The praisonai call CLI always loads .env from the current directory, no flag needed.


How It Works

The CLI searches upward from where you run it, never from where PraisonAI is installed.

Diagram
Agentos.environfind_dotenv(usecwd=True)praisonai CLIYouAgentos.environfind_dotenv(usecwd=True)praisonai CLIYoupraisonai run “hello” (in cwd/)search from cwd upwardfirst .env foundload, override=Falsemerged environment

Which Entry Points Auto-Load .env

Each CLI entry point loads .env from the current directory; library imports never do.

Entry pointLoads .env from CWD?Notes
praisonai run / praisonai chat / praisonai code / direct promptYesvia _load_env_once() on CLI startup
praisonai call CLIYesexplicit load in main()
praisonai botYesvia BotHandler._load_dotenv
import praisonai.api.call (module import)Noopt in with PRAISONAI_CALL_LOAD_DOTENV=true
from praisonaiagents import Agent (library import)Nolibrary imports never mutate os.environ; call load_dotenv() yourself

Do I Need to Load .env Manually?

Diagram

Using the praisonai CLI

Importing praisonaiagents

Importing praisonai.api.call

Do I need to load .env manually?

✅ No — automatic from CWD

📝 Yes — call load_dotenv

⚙️ Set PRAISONAI_CALL_LOAD_DOTENV=true

For library use:

python
from dotenv import load_dotenv
from praisonaiagents import Agent

load_dotenv()

agent = Agent(name="Helper", instructions="You are a helpful assistant")
agent.start("hello")

Precedence

Loading never overrides a variable that is already set (override=False).

SourceWins overExample
Already-exported shell env var.envexport OPENAI_API_KEY=sk-shell beats a .env value
.env in CWD.env higher up the tree./.env is read; ../.env is not
Any .env above the install prefix—Never loaded, by design

Best Practices

Put .env in your project root

Keep it in the directory you run praisonai from, so the CLI always finds it.

Never commit .env

Add .env to .gitignore so keys stay out of version control.

Prefer shell exports for secrets in CI

.env is a developer convenience, not a secrets manager. Export variables from your CI secret store instead.

Library imports do not touch os.environ

When embedding praisonaiagents or importing praisonai.api.call directly, call from dotenv import load_dotenv; load_dotenv() yourself. See Python Import Safety.


Related