Skip to content

Error in loading env variables #42823

@rcarlier

Description

@rcarlier

MDN URL

https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Server-side/Django/Deployment

What specific section or headline is this issue about?

Getting your website ready to publish

What information was incorrect, unhelpful, or incomplete?

import os
from dotenv import load_dotenv
env_path = load_dotenv(os.path.join(BASE_DIR, '.env'))
load_dotenv(env_path)

load_dotenv() returns a boolean, not a path. Calling load_dotenv(env_path) therefore passes a boolean instead of a file path and does nothing useful.

What did you expect to see?

Correct approach:

from pathlib import Path
from dotenv import load_dotenv

BASE_DIR = Path(__file__).resolve().parent.parent
ENV_FILE = BASE_DIR / ".env"

# Load .env only if it exists; otherwise rely on system environment variables
if ENV_FILE.exists():
    load_dotenv(ENV_FILE)

Do you have any supporting links, references, or citations?

Generate an error like:

<frozen genericpath>:39: RuntimeWarning: bool is used as a file descriptor
/path/to/venv/lib/python3.14/site-packages/dotenv/main.py:431: RuntimeWarning: bool is used as a file descriptor
  st = os.stat(path)

Do you have anything more you want to share?

No response

MDN metadata

Page report details

Metadata

Metadata

Assignees

Labels

Content:LearnLearning area docsneeds triageTriage needed by staff and/or partners. Automatically applied when an issue is opened.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions