惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
T
Tailwind CSS Blog
D
Docker
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
腾讯CDC
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
U
Unit 42
The Cloudflare Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
爱范儿
爱范儿
Recent Announcements
Recent Announcements
博客园 - 聂微东
博客园 - 叶小钗
H
Help Net Security
MyScale Blog
MyScale Blog

博客园 - 漫思

python项目的构建 nodejs构建CICD时的思考  成为 AI 智能体工程师的 10 个步骤 es6的 yield python 中的 yield 笔记本的A壳 thinkpad 更换 reduce() in Python python的字段赋值和取值的操作 python的语法类似于lodash分组展开,合并分组操作 SelectMany C# lodash 数组的常用做法 lodash里面的常用方法 技术的边界 Reduce 和 Transduce 的含义 尤雨溪创办的 VoidZero 官宣加入 Cloudflare,前端 Vite 等保持开源 Ramda 函数库参考教程 ramda es 数组的方法 flatmap map object.entity的教程 Map与FlatMap:在数据处理中的区别与联系 flat、flatmap与map的用法区别1 flat、flatmap与map的用法区别 AI不是从天而降,它经历了七十年三起三落:读懂AI的第三课 Agent 17 种架构模式 分析 & 思考 只有踩过坑才懂:前端生成唯一 ID,别用 Date.now ()了!试试它crypto.randomUUID() FastAPI python并发 代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering 高管的 AI 精神病
Python and .env Files
漫思 · 2026-06-17 · via 博客园 - 漫思

Python and .env Files

By default, Python does not automatically read environment variables from a .env file. The interpreter only accesses environment variables already set in the operating system. If you want Python to load variables from a .env file, you need to explicitly use a library such as python-dotenv to handle this process.

A .env file is typically used to store sensitive configuration values like API keys, database credentials, or environment-specific settings. This keeps them out of your source code and allows easy switching between environments.

Example workflow using python-dotenv:

# Install the library first:

# pip install python-dotenv

import os

from dotenv import load_dotenv

# Load variables from .env in the current directory

load_dotenv()

# Retrieve environment variables

db_host = os.getenv("DB_HOST", "localhost")

api_key = os.getenv("API_KEY")

print(f"Database Host: {db_host}")

print(f"API Key: {api_key}")

Steps to implement:

  • Install python-dotenv with pip install python-dotenv.

  • Create a .env file in your project root:

DB_HOST=127.0.0.1

API_KEY=your_api_key_here

  • Load it in your Python code using load_dotenv().

  • Access variables via os.getenv() or os.environ.get().

Best practices:

  • Never commit .env files to version control; add them to .gitignore.

  • Maintain a .env.example template with placeholder values for team members.

  • Use different .env files for development, testing, and production to simplify environment switching.

Key takeaway: Without python-dotenv or similar tools, Python will not read .env files automatically. You must explicitly load them into the environment before use, ensuring both security and flexibility in configuration management.

漫思

posted on 2026-06-17 15:18  漫思  阅读(0)  评论()    收藏  举报