Skip to main content

Bazi

面向中文用户的 Python 3.11+ 八字排盘库,提供四柱、五行、十神、纳音、关系与流运查询。 也可不创建命盘,直接查阅知识条目、原文出处与适用限度。运行时只用标准库及随包数据,不联网。

安装与第一张盘

python -m pip install bazi

下面的基本排盘示例可独立运行。本文及下方各指南对应当前源码;已发布版本以对应 tag 下的说明为准。 从源码试用当前接口时,在仓库根目录运行 python -m pip install .。

from datetime import datetime

from bazi.bazi import Bazi
from bazi.bazi_chart import BaziChart

bazi = Bazi.create(datetime(1990, 5, 17, 10, 30), 'female')
chart = BaziChart(bazi)

print('四柱', ' '.join(str(pillar) for pillar in bazi.pillars))
print('日主', bazi.day_master)
print('性别', chart.json['gender'])
print('精度与后端', chart.json['precision'], chart.json['backend'])
四柱 庚午 辛巳 壬午 乙巳
日主 壬
性别 female
精度与后端 day celestial

类型从定义模块导入;安装包没有根类入口或命令行程序。领域名称用拼音,枚举另有中文别名。

按任务继续

想做什么 从这里开始
排第一张盘,了解默认值和输出 快速开始
改精度、换日点、真太阳时或流派取法 配置与时间口径
查原局与流运的关系,取有限几步大运和流年 关系与流运
不排盘,只查条目、来源、过滤和导出重载 独立知识库指南
导入、JSON 恢复、前提匹配与记录兼容 集成开发、接口与记录契约
按对象、来源、情境或判据浏览知识 知识目录
逐模块查签名、参数与条件 文档入口,构建后进入 API Reference
提交勘误、出处或流派读法 贡献指南

完整文档由源码与知识注册表生成;构建方法见开发。安装包带有 py.typed 内联类型标注。

能力与解释边界

默认使用 celestial 后端、DAY 精度、晚子时 23:00 换日柱;这些是选定的口径,不是唯一读法。 BaziSchool 保留关系、神煞与配偶星等不同取法,出处随字段说明保留。需要比较其他排盘器时,见外部排盘对照。

知识条目分别记录来源状态、输出资格和前提整理状态。来源已核定不证明条文本身为真, 也不证明适用于某张命盘。前提判别不计算旺衰、喜忌、格局或现实事件;部分判据只检查前提的一部分,限度见知识目录。

三类 JSON 的恢复保证不同:命盘重算并核对,知识库恢复内容,前提判别记录只校验记录结构而不重判。 详细条件见接口与记录契约;升级变化见发布说明。

源码脚本

以下脚本需先按开发安装依赖,在仓库根目录运行,不是安装包的命令行入口。

命令 输出
python run_demo.py 命盘、大运/小运/流年及命盘 JSON
python run_relationship_analyzer.py 夫妻宫、配偶星、原局神煞及十年流年神煞
python run_interpreter.py 单盘静态说明,默认不导出文件

三者默认生成随机命盘。固定出生,显示参考文字及来源:

python run_interpreter.py \
  --birth-time "2000-01-01 12:00" \
  --gender male \
  --include-reference-only \
  --show-sources

不输入出生信息,查询知识并导出所选条目:

python run_interpreter.py \
  --query-knowledge \
  --object tiangan.ding \
  --include-reference-only \
  --export-knowledge-json output_data/knowledge.json

检查前提并导出记录:

python run_interpreter.py \
  --match-context editorial.guansha_coexistence.v1 \
  --birth-time "2000-01-01 12:00" \
  --gender female \
  --observation-scope natal_and_liunian \
  --ganzhi-year 2024 \
  --include-reference-only \
  --export-context-json output_data/context.json

随机示例用 --seed 重放;全部参数见 python run_interpreter.py --help。 命盘展示中的十神百分比只统计三个非日主明干与四支主气位置,不代表旺衰或力量。 JSON 导出拒绝覆盖知识源;用 --output-dir 导出命盘 TXT 时,会覆盖 interpretation_examples/{序号}.txt 路径上已有的同名文件,其他文件保留。

开发

在独立 Python 环境中安装开发与文档依赖,运行完整检查:

python -m pip install -r Requirements.txt
python -m pip install -r docs/requirements.txt
python -m pip install celestial-calendar==0.6.1
python run_tests.py -a -v

完整检查包含所有测试及 100% 库覆盖率、ruff、mypy、示例、python -O 输入检查、安装工件验证和文档检查。 裸 python run_tests.py 不含 slow/hkodata 和可选任务,不能替代完整检查。 工件验证可能联网下载构建及类型工具;源码示例可能写入 output_data/。

只构建并检查文档:

python run_docs.py

检查严格 HTML 构建、API 地址、完整知识目录、内部链接与教程示例;通过后可本地打开 output_data/docs/html/index.html。 构建命令不部署站点,也不启用 Pages。更多测试选项见 python run_tests.py --help,外部贡献要求见贡献指南。

保留已验证的安装包

python run_package_checks.py --output-dir ../bazi-tested
python -m pip install ../bazi-tested/bazi-*-py3-none-any.whl

输出目录须为空且在仓外。发布步骤见发布流程。 重建历法表见 HKO 编码器、天文表生成器 及数据格式。安装后缺表应重装包,不运行生成器修补。

贡献与许可

本库由个人维护,接受 issue 勘误与出处补充;PR 先开 issue 讨论,方向获同意后再提交,要求见贡献指南。

作者自有材料采用标准 MIT License。第三方材料说明 另列历法数据、引文等权利范围, 不由作者的 MIT 授权重新许可;两份声明都随工件分发。

Metadata

Release files for bazi 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bazi 2.1.0
File Size Uploaded Provenance Actions
bazi-2.1.0.tar.gz 1.3 MB With provenance Details for bazi-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bazi 2.1.0
File Interpreter ABI Platform Actions
bazi-2.1.0-py3-none-any.whl Python 3 none any Details for bazi-2.1.0-py3-none-any.whl

Total release size: 1.7 MB

Release files / bazi-2.1.0.tar.gz

Download URL bazi-2.1.0.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
f9aca8a654285afeb3c9acdbdbb8db7895f024a4334fb0826bfecbfc3e37662d
BLAKE2b-256 checksum
How to use checksums
59cbf381c0ace8b74291f63aee3b2eef0335ea6ab98d649e99ee03edd79cce93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Release files / bazi-2.1.0-py3-none-any.whl

Download URL bazi-2.1.0-py3-none-any.whl
Size 407.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9f746e6da85e70dee225fc711186faf1ff3e23295588a233b4b2737bd7f4b10
BLAKE2b-256 checksum
How to use checksums
87bb2f09672cbb9cd24cd02ad430720baf65de1b6df02de987389992bb26564c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

Security for bazi 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bazi-2.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page