最近在樹莓派 Pi 400 與 Pi 3A+ 的 Trixie 與 Trixie Lite 作業系統上安裝 Python 套件時, 發現我之前寫的 kbar 套件指定所依賴的 numpy 須不高於 1.23 版沒有必要 (pandas 與 matplotlib 也是), 那時不知哪裡得到錯誤訊息 (可能是 ChatGPT), 誤以為此要求來自 mplfinance, 才將 numpy 等版本上限寫進 dependencies 條件裡, 實測發現 mplfinance 在這些依賴套件的最新版中可順利執行, 於是將 kbar 套件升版為 v0.1.4 版 (原本是 v0.1.3, 但這版本號被我上傳 TestPyPI 時用掉了, 只好再跳一號), 主要就是移除 numpy, pandas, 與 matplotlib 的版本上限, 附帶一些與功能無關的小修正.
更新檔案與發佈上傳程序主要參考第三篇.
1. 修改 project.toml :
移除 numpy, pandas, 與 matplotlib 上限, 並於 project.urls 項目下添加 Documentation 參數 URL, 指向 GitHub 上的 README.md 檔, 修改後內容如下 (黃底色為修改或增加部分) :
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "kbar"
version = "0.1.4"
description = "A lightweight K-line (candlestick) plotting tool with matplotlib and mplfinance."
readme = "README.md"
requires-python = ">=3.9"
license = { file = "LICENSE" }
authors = [
{ name = "Tony Y.H. Huang" }
]
keywords = ["finance", "candlestick", "mplfinance", "k-line", "stock"]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Intended Audience :: Financial and Insurance Industry",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent"
]
dependencies = [
"numpy>=1.23",
"pandas>=2.0",
"matplotlib>=3.7",
"mplfinance>=0.12.10b0",
"pyarrow>=14.0.2"
]
[project.optional-dependencies]
dev = [
"pytest>=8.0",
"coverage>=7.0"
]
[tool.pytest.ini_options]
addopts = "-v --maxfail=3 --disable-warnings"
testpaths = ["tests"]
[tool.coverage.run]
branch = true
source = ["kbar"]
[tool.coverage.report]
show_missing = true
skip_covered = true
[tool.setuptools.packages.find]
where = ["src"]
[project.urls]
"Homepage" = "https://github.com/tony1966/kbar/"
"Bug Tracker" = "https://github.com/tony1966/kbar/issues"
"Documentation" = "https://github.com/tony1966/kbar/#readme"
2. 修改 src/kbar/__init__.py :
添加了 __version__ 參數與前面的套件說明, 這樣匯入套件後就可以用 __version__ 屬性查看版本號 :
"""
KBar: A lightweight stock candlestick plotting utility with Chinese font auto-detection.
"""
from .kbar import KBar, detect_font
__all__ = ["KBar", "detect_font"]
__version__ = "0.1.4"
3. 新增版本異動記錄檔 CHANGELOG.md :
用來記錄升版修正與共能異動 :
\# Changelog
All notable changes to this project will be documented in this file.
The format is based on \[Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to \[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
---
\## \[0.1.4] - 2025-11-22
\### Changed
\- 移除對 `numpy <2` 的不必要限制(允許使用 `numpy>=1.23`)。
\- 移除對 `pandas <3.0` 的不必要限制(允許使用 `pandas>=2.0`)。
\- 移除對 `matplotlib <4.0` 的不必要限制(允許使用 `matplotlib>=3.7`)。
---
4. 清理專案資料夾 & Build 專案 :
先將專案資料夾下的下列子目錄刪除 (如果有的話) :
- __pycache__ : 存放 Python 產生的 bytecode 暫存檔的目錄
- build : 執行 python -m build 生成的中間建構資料夾
- dist : 上次打包生成的 wheel 與 sdist
- *.egg-info : setuptools 生成的套件資訊資料夾 (在 src 資料夾下)
清理完上一版的發佈資料後, 用 tree 指令查看是否如下乾淨 :
D:\PyPi>tree kbar /f
Folder PATH listing for volume 新增磁碟區
Volume serial number is 1258-16B8
D:\PYPI\KBAR
│ .gitignore
│ CHANGELOG.md
│ LICENSE
│ pyproject.toml
│ README.md
│
├───src
│ └───kbar
│ kbar.py
│ __init__.py
│
└───tests
test_kbar.py
__init__.py
這樣就可以進入專案資料夾 kbar 用 python -m build 指令打包專案 :
D:\PyPi>cd kbar
D:\PyPi\kbar>python -m build
... (略) ...
Copying src\kbar.egg-info to build\bdist.win-amd64\wheel\.\kbar-0.1.4-py3.10.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\kbar-0.1.4.dist-info\WHEEL
creating 'D:\PyPi\kbar\dist\.tmp-zbsfxpp2\kbar-0.1.4-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'kbar/__init__.py'
adding 'kbar/kbar.py'
adding 'kbar-0.1.4.dist-info/licenses/LICENSE'
adding 'kbar-0.1.4.dist-info/METADATA'
adding 'kbar-0.1.4.dist-info/WHEEL'
adding 'kbar-0.1.4.dist-info/top_level.txt'
adding 'kbar-0.1.4.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built kbar-0.1.4.tar.gz and kbar-0.1.4-py3-none-any.whl
這樣就得到 .gz (原始碼) 與 .whl (安裝檔) 這兩個打包後結果了.
跳回上一層檢視 build 後的檔案目錄結構 :
d:\PyPi\kbar>cd ..
d:\PyPi>tree kbar /f
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\PYPI\KBAR
│ .gitignore
│ CHANGELOG.md
│ LICENSE
│ pyproject.toml
│ README.md
│
├─dist
│ kbar-0.1.4-py3-none-any.whl
│ kbar-0.1.4.tar.gz
│
├─src
│ ├─kbar
│ │ kbar.py
│ │ __init__.py
│ │
│ └─kbar.egg-info
│ dependency_links.txt
│ PKG-INFO
│ requires.txt
│ SOURCES.txt
│ top_level.txt
│
└─tests
test_kbar.py
__init__.py
5. 安裝 whl 檔進行發佈前測試 :
先用 pip uninstall kbar 解壓縮之前本機安裝的 kbar, 然後安裝上面打包的 whl 檔 :
d:\PyPi\kbar>pip install dist\kbar-0.1.4-py3-none-any.whl
Processing d:\pypi\kbar\dist\kbar-0.1.4-py3-none-any.whl
Requirement already satisfied: numpy>=1.23 in c:\users\tony1\appdata\local\programs\python\python312\lib\site-packages (from kbar==0.1.4) (1.26.4)
Requirement already satisfied: pandas>=2.0 in c:\users\tony1\appdata\local\programs\python\python312\lib\site-packages (from kbar==0.1.4) (2.3.1)
Requirement already satisfied: matplotlib>=3.7 in c:\users\tony1\appdata\local\programs\python\python312\lib\site-packages (from kbar==0.1.4) (3.10.5)
Requirement already satisfied: mplfinance>=0.12.10b0 in c:\users\tony1\appdata\local\programs\python\python312\lib\site-packages (from kbar==0.1.4) (0.12.10b0)
... (略) ...
Requirement already satisfied: six>=1.5 in c:\users\tony1\appdata\local\programs\python\python312\lib\site-packages (from python-dateutil>=2.7->matplotlib>=3.7->kbar==0.1.4) (1.16.0)
Installing collected packages: kbar
Successfully installed kbar-0.1.4
進入 REPL 測試 __version__ 屬性 :
d:\PyPi\kbar>python
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import kbar
>>> kbar.__version__
'0.1.4'
用下列程式測試是否可正常繪製 K 線圖 :
# kbar_test.py
from kbar import KBar
import yfinance as yf
df=yf.download('0050.TW', start='2024-07-01', end='2024-08-21', auto_adjust=False)
df.columns=df.columns.map(lambda x: x[0]) # 改成舊版單層索引
kb=KBar(df) # 未傳 font 參數預設使用正黑體
kb.plot(title='台灣五十(0050.TW)', volume=True)
功能看來沒問題.
6. 發佈到 PyPI 升版 :
利用 twine 套件將專案上傳到 PyPI :
D:\PyPi\kbar>python -m twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your API token: (按 Ctrl+V 貼上 PyPI API Token 按 Enter )
Uploading kbar-0.1.4-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.3/17.3 kB • 00:00 • ?
Uploading kbar-0.1.4.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.2/19.2 kB • 00:00 • ?
View at:
這樣便完成新版 kbar 上傳發佈作業了.
先用 pip uninstall kbar 解除安裝本機舊版 kbar, 然後重新安裝 kbar :
d:\python\test>pip install kbar
... (略) ...
Requirement already satisfied: pillow>=8 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib>=3.7->kbar) (11.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib>=3.7->kbar) (3.2.3)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib>=3.7->kbar) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas>=2.0->kbar) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas>=2.0->kbar) (2025.2)
Requirement already satisfied: six>=1.5 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from python-dateutil>=2.7->matplotlib>=3.7->kbar) (1.17.0)
Downloading kbar-0.1.4-py3-none-any.whl (7.9 kB)
Installing collected packages: kbar
Successfully installed kbar-0.1.4
可見不指定版本預設會從 PyPI 下載最新版 v0.1.4 來安裝, 執行上面的 kbar_test.py :
OK 收工啦!
2025-11-23 補充 :
我在 Pi 400 安裝 kbar 檢查版本號, 才發現 __init__.py 裡面的 __version__ 屬性忘記改, 還是顯示 0.1.3, 啊! 讓完美主義的我捶心肝 :
(myenv313) pi@raspberrypi:~ $ pip install kbar
Collecting kbar
Downloading kbar-0.1.4-py3-none-any.whl.metadata (6.1 kB)
Requirement already satisfied: numpy>=1.23 in ./myenv313/lib/python3.13/site-packages (from kbar) (2.2.6)
Requirement already satisfied: pandas>=2.0 in ./myenv313/lib/python3.13/site-packages (from kbar) (2.3.3)
... (略) ...
Requirement already satisfied: six>=1.5 in ./myenv313/lib/python3.13/site-packages (from python-dateutil>=2.7->matplotlib>=3.7->kbar) (1.17.0)
Downloading kbar-0.1.4-py3-none-any.whl (7.9 kB)
Downloading pyarrow-22.0.0-cp313-cp313-manylinux_2_28_aarch64.whl (45.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.0/45.0 MB 641.4 kB/s eta 0:00:00
Installing collected packages: pyarrow, kbar
Successfully installed kbar-0.1.4 pyarrow-22.0.0
(myenv313) pi@raspberrypi:~ $ python
Python 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import kbar
>>> kbar.__version__
'0.1.3'
算了, 就當作是小 Bug 吧! 反正 v0.1.4 其實就是 v0.1.3, 只是因為 TestPyPI 佔了版本號而已.