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

推荐订阅源

G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
I
InfoQ
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
B
Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
量子位
Hugging Face - Blog
Hugging Face - 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)  评论()    收藏  举报