feature-flags-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostHog feature flags for Python
Python应用的PostHog功能开关
This skill helps you add PostHog feature flags to Python applications.
本技能可帮助你在Python应用中添加PostHog功能开关。
Reference files
参考文件
- - Python feature flags installation - docs
references/python.md - - Adding feature flag code - docs
references/adding-feature-flag-code.md - - Feature flag best practices - docs
references/best-practices.md
Consult the documentation for API details and framework-specific patterns.
- - Python功能开关安装文档
references/python.md - - 添加功能开关代码文档
references/adding-feature-flag-code.md - - 功能开关最佳实践文档
references/best-practices.md
如需API详情及特定框架的实现模式,请查阅相关文档。
Key principles
核心原则
- Environment variables: Always use environment variables for PostHog keys. Never hardcode them.
- Minimal changes: Add feature flag code alongside existing logic. Don't replace or restructure existing code.
- Boolean flags first: Default to boolean flag checks unless the user specifically asks for multivariate flags.
- Server-side when possible: Prefer server-side flag evaluation to avoid UI flicker.
- 环境变量:始终使用环境变量存储PostHog密钥,绝不要硬编码。
- 最小变更:在现有逻辑旁添加功能开关代码,不要替换或重构现有代码。
- 优先布尔型开关:默认使用布尔型开关检查,除非用户明确要求多变量开关。
- 尽可能服务端评估:优先选择服务端开关评估,以避免UI闪烁。
PostHog MCP tools
PostHog MCP工具
Check if a PostHog MCP server is connected. If available, look for tools related to feature flag management (creating, listing, updating, deleting flags). Use these tools to manage flags directly in PostHog rather than requiring the user to do it manually in the dashboard.
检查是否已连接PostHog MCP服务器。若已连接,查找与功能开关管理相关的工具(创建、列出、更新、删除开关)。使用这些工具直接在PostHog中管理开关,无需用户手动在控制台操作。
Framework guidelines
框架指南
- Remember that source code is available in the venv/site-packages directory
- posthog is the Python SDK package name
- Install dependencies with or
pip install posthogand do NOT use unquoted version specifiers likepip install -r requirements.txtdirectly in shell commands>= - In CLIs and scripts: MUST call posthog.shutdown() before exit or all events are lost
- Always use the Posthog() class constructor (instance-based API) instead of module-level posthog.api_key config
- Always include enable_exception_autocapture=True in the Posthog() constructor to automatically track exceptions
- NEVER send PII in capture() event properties — no emails, full names, phone numbers, physical addresses, IP addresses, or user-generated content
- PII belongs in identify() person properties, NOT in capture() event properties. Safe event properties are metadata like message_length, form_type, boolean flags.
- Register posthog_client.shutdown with atexit.register() to ensure all events are flushed on exit
- The Python SDK has NO identify() method — use posthog_client.set(distinct_id=user_id, properties={...}) to set person properties, or use identify_context(user_id) within a context
- 请记住源代码位于venv/site-packages目录中
- posthog是Python SDK的包名
- 使用或
pip install posthog安装依赖,不要在shell命令中直接使用无引号的版本说明符(如pip install -r requirements.txt)>= - 在CLI和脚本中:退出前必须调用posthog.shutdown(),否则所有事件都会丢失
- 始终使用Posthog()类构造函数(基于实例的API),而非模块级别的posthog.api_key配置
- 在Posthog()构造函数中始终包含enable_exception_autocapture=True,以自动追踪异常
- 绝不要在capture()事件属性中发送个人身份信息(PII)——包括邮箱、全名、电话号码、物理地址、IP地址或用户生成内容
- 个人身份信息应放在identify()用户属性中,而非capture()事件属性中。安全的事件属性包括元数据,如message_length、form_type、布尔型开关等。
- 使用atexit.register()注册posthog_client.shutdown,确保退出时所有事件都能被刷新
- Python SDK没有identify()方法——使用posthog_client.set(distinct_id=user_id, properties={...})设置用户属性,或在上下文内使用identify_context(user_id)