2025年8月21日 星期四

Python 學習筆記 : 將 K 線圖模組 kbar.py 打包上傳 PyPI (三)

在前兩篇測試中已完成 kbar 專案打包前的準備工作, 本篇要正式進入打包與發佈程序了, 但在正式發佈到 PyPI 前會先發佈到 TestPyPI 網站, 這個測試網站是 PyPI 的分身, 不僅網站外觀與 PyPI 長得一模一樣, 操作方式與運作規則也幾乎完全一樣, 設置 TestPyPI 的目的是讓我們在正式發佈前在此做模擬發佈熟悉流程, 它與 PyPI 唯一的差別是發佈到 PyPI 的套件無法刪除; 而 TestPyPI 則允許刪除 (但用過的版本號碼同樣不能再使用, 必須升版).

本系列之前的文章參考 :



10. 安裝打包與上傳工具 :  

用 pip 指令安裝 build 與 twine 套件 :

pip install --upgrade build twine

其中 build 用來將 Python 專案打包成可上傳到 PyPI 的格式 (二進位套件 wheel 與原始碼套件 sdist); 而 twine 則是用來將已打包好的套件上傳到 PyPI (官方套件庫) 或測試用的 TestPyPI 套件庫. 

D:\PyPi>pip install --upgrade build twine  
Collecting build
  Downloading build-1.3.0-py3-none-any.whl.metadata (5.6 kB)
Requirement already satisfied: twine in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (5.0.0)
Collecting twine
  Downloading twine-6.1.0-py3-none-any.whl.metadata (3.7 kB)
Requirement already satisfied: packaging>=19.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from build) (24.2)
Collecting pyproject_hooks (from build)
  Downloading pyproject_hooks-1.2.0-py3-none-any.whl.metadata (1.3 kB)
Requirement already satisfied: colorama in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from build) (0.4.6)
Requirement already satisfied: tomli>=1.1.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from build) (2.0.1)
Requirement already satisfied: readme-renderer>=35.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from twine) (43.0)
Requirement already satisfied: requests>=2.20 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from twine) (2.31.0)
Requirement already satisfied: requests-toolbelt!=0.9.0,>=0.8.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from twine) (1.0.0)
Requirement already satisfied: urllib3>=1.26.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from twine) (1.26.19)
Requirement already satisfied: keyring>=15.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from twine) (24.3.1)
Requirement already satisfied: rfc3986>=1.4.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from twine) (2.0.0)
Requirement already satisfied: rich>=12.0.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from twine) (13.5.3)
Collecting id (from twine)
  Downloading id-1.5.0-py3-none-any.whl.metadata (5.2 kB)
Requirement already satisfied: jaraco.classes in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from keyring>=15.1->twine) (3.3.1)
Requirement already satisfied: importlib-metadata>=4.11.4 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from keyring>=15.1->twine) (6.8.0)
Requirement already satisfied: pywin32-ctypes>=0.2.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from keyring>=15.1->twine) (0.2.2)
Requirement already satisfied: zipp>=0.5 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from importlib-metadata>=4.11.4->keyring>=15.1->twine) (3.17.0)
Requirement already satisfied: nh3>=0.2.14 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from readme-renderer>=35.0->twine) (0.2.15)
Requirement already satisfied: docutils>=0.13.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from readme-renderer>=35.0->twine) (0.20.1)
Requirement already satisfied: Pygments>=2.5.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from readme-renderer>=35.0->twine) (2.16.1)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests>=2.20->twine) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests>=2.20->twine) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from requests>=2.20->twine) (2025.6.15)
Requirement already satisfied: markdown-it-py>=2.2.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from rich>=12.0.0->twine) (3.0.0)
Requirement already satisfied: mdurl~=0.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from markdown-it-py>=2.2.0->rich>=12.0.0->twine) (0.1.2)
Requirement already satisfied: more-itertools in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from jaraco.classes->keyring>=15.1->twine) (10.2.0)
Downloading build-1.3.0-py3-none-any.whl (23 kB)
Downloading twine-6.1.0-py3-none-any.whl (40 kB)
Downloading id-1.5.0-py3-none-any.whl (13 kB)
Downloading pyproject_hooks-1.2.0-py3-none-any.whl (10 kB)
Installing collected packages: pyproject_hooks, id, build, twine
  Attempting uninstall: twine
    Found existing installation: twine 5.0.0
    Uninstalling twine-5.0.0:
      Successfully uninstalled twine-5.0.0
Successfully installed build-1.3.0 id-1.5.0 pyproject_hooks-1.2.0 twine-6.1.0

  
11. 用 build 工具打包套件生成發佈檔 :

在進行套件打包前要先清理專案資料夾下面的暫存檔與可能的舊打包檔, 把不需要打包上傳到 PyPI 的資料刪除. 檢視專案中是否有下列這些子目錄, 有的話要移除 :
  • __pycache__ : 存放 Python 產生的 bytecode 暫存檔的目錄
  • build : 執行 python -m build 生成的中間建構資料夾
  • dist : 上次打包生成的 wheel 與 sdist
  • *.egg-info : setuptools 生成的套件資訊資料夾
可用 tree 指令檢視檔案樹狀結構是否已清乾淨 : 

D:\PyPi>tree kbar /f  
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\PYPI\KBAR
│  .gitignore
│  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   
D:\PyPi\kbar>python -m build
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools>=61.0
  - wheel
* Getting build dependencies for sdist...
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:61: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  dist._finalize_license_expression()
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
running egg_info
creating src\kbar.egg-info
writing src\kbar.egg-info\PKG-INFO
writing dependency_links to src\kbar.egg-info\dependency_links.txt
writing requirements to src\kbar.egg-info\requires.txt
writing top-level names to src\kbar.egg-info\top_level.txt
writing manifest file 'src\kbar.egg-info\SOURCES.txt'
reading manifest file 'src\kbar.egg-info\SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'src\kbar.egg-info\SOURCES.txt'
* Building sdist...
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:61: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  dist._finalize_license_expression()
C:\Users\tony1\AppData\Local\Temp\build-env-cvzjrnch\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
running sdist
running egg_info
writing src\kbar.egg-info\PKG-INFO
writing dependency_links to src\kbar.egg-info\dependency_links.txt
writing requirements to src\kbar.egg-info\requires.txt
writing top-level names to src\kbar.egg-info\top_level.txt
reading manifest file 'src\kbar.egg-info\SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'src\kbar.egg-info\SOURCES.txt'
running check
creating kbar-0.1.2
creating kbar-0.1.2\src\kbar
creating kbar-0.1.2\src\kbar.egg-info
creating kbar-0.1.2\tests
copying files to kbar-0.1.2...
copying LICENSE -> kbar-0.1.2
copying README.md -> kbar-0.1.2
copying pyproject.toml -> kbar-0.1.2
copying src\kbar\__init__.py -> kbar-0.1.2\src\kbar
copying src\kbar\kbar.py -> kbar-0.1.2\src\kbar
copying src\kbar.egg-info\PKG-INFO -> kbar-0.1.2\src\kbar.egg-info
copying src\kbar.egg-info\SOURCES.txt -> kbar-0.1.2\src\kbar.egg-info
copying src\kbar.egg-info\dependency_links.txt -> kbar-0.1.2\src\kbar.egg-info
copying src\kbar.egg-info\requires.txt -> kbar-0.1.2\src\kbar.egg-info
copying src\kbar.egg-info\top_level.txt -> kbar-0.1.2\src\kbar.egg-info
copying tests\test_kbar.py -> kbar-0.1.2\tests
copying src\kbar.egg-info\SOURCES.txt -> kbar-0.1.2\src\kbar.egg-info
Writing kbar-0.1.2\setup.cfg
Creating tar archive
removing 'kbar-0.1.2' (and everything under it)
* Building wheel from sdist
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools>=61.0
  - wheel
* Getting build dependencies for wheel...
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:61: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  dist._finalize_license_expression()
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
running egg_info
writing src\kbar.egg-info\PKG-INFO
writing dependency_links to src\kbar.egg-info\dependency_links.txt
writing requirements to src\kbar.egg-info\requires.txt
writing top-level names to src\kbar.egg-info\top_level.txt
reading manifest file 'src\kbar.egg-info\SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'src\kbar.egg-info\SOURCES.txt'
* Building wheel...
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated
!!

        ********************************************************************************
        Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0).

        By 2026-Feb-18, you need to update your project and remove deprecated calls
        or your builds will no longer be supported.

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  corresp(dist, value, root_dir)
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:61: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  dist._finalize_license_expression()
C:\Users\tony1\AppData\Local\Temp\build-env-tljli9vc\lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated.
!!

        ********************************************************************************
        Please consider removing the following classifiers in favor of a SPDX license expression:

        License :: OSI Approved :: MIT License

        See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details.
        ********************************************************************************

!!
  self._finalize_license_expression()
running bdist_wheel
running build
running build_py
creating build\lib\kbar
copying src\kbar\kbar.py -> build\lib\kbar
copying src\kbar\__init__.py -> build\lib\kbar
running egg_info
writing src\kbar.egg-info\PKG-INFO
writing dependency_links to src\kbar.egg-info\dependency_links.txt
writing requirements to src\kbar.egg-info\requires.txt
writing top-level names to src\kbar.egg-info\top_level.txt
reading manifest file 'src\kbar.egg-info\SOURCES.txt'
adding license file 'LICENSE'
writing manifest file 'src\kbar.egg-info\SOURCES.txt'
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64\wheel
creating build\bdist.win-amd64\wheel\kbar
copying build\lib\kbar\kbar.py -> build\bdist.win-amd64\wheel\.\kbar
copying build\lib\kbar\__init__.py -> build\bdist.win-amd64\wheel\.\kbar
running install_egg_info
Copying src\kbar.egg-info to build\bdist.win-amd64\wheel\.\kbar-0.1.2-py3.10.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\kbar-0.1.2.dist-info\WHEEL
creating 'D:\PyPi\kbar\dist\.tmp-gf9zd61n\kbar-0.1.2-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.2.dist-info/licenses/LICENSE'
adding 'kbar-0.1.2.dist-info/METADATA'
adding 'kbar-0.1.2.dist-info/WHEEL'
adding 'kbar-0.1.2.dist-info/top_level.txt'
adding 'kbar-0.1.2.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built kbar-0.1.2.tar.gz and kbar-0.1.2-py3-none-any.whl

這樣便成功地完成打包作業了, 切回專案資料夾上一層再次用 tree 指令檢視檔案樹 :

D:\PyPi\kbar>cd ..  
D:\PyPi>tree kbar /f  
列出磁碟區 新增磁碟區 的資料夾 PATH
磁碟區序號為 1258-16B8
D:\PYPI\KBAR
│  .gitignore
│  LICENSE
│  pyproject.toml
│  README.md
├─dist
│      kbar-0.1.2-py3-none-any.whl
│      kbar-0.1.2.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

可見打包程式 build 在專案資料夾下新增了一個 dist 資料夾來存放要發佈到 PyPI 的兩個檔案 (wheel 二進位檔與 gz 壓縮檔), 另外在 src 下則建立了一個 kbar.egg-info 子目錄來存放套件資訊. 

 
12. 用生成的 wheel 檔測試本地安裝 :

在正式發佈套件前, 先用上面 build 工具生成的發佈檔中的二進位 wheel 檔 kbar-0.1.2-py3-none-any.whl 在本機安裝打包好的套件, 看看是否可正常使用 :

D:\PyPi\kbar>pip install dist/kbar-0.1.2-py3-none-any.whl     
Processing d:\pypi\kbar\dist\kbar-0.1.2-py3-none-any.whl
Collecting numpy<2 (from kbar==0.1.2)
  Downloading numpy-1.26.4-cp310-cp310-win_amd64.whl.metadata (61 kB)
Requirement already satisfied: pandas<3.0,>=2.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.2) (2.3.1)
Requirement already satisfied: matplotlib<4.0,>=3.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.2) (3.10.5)
Requirement already satisfied: mplfinance>=0.12.10b0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.2) (0.12.10b0)
Requirement already satisfied: pyarrow>=14.0.2 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from kbar==0.1.2) (21.0.0)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (1.3.2)
Requirement already satisfied: cycler>=0.10 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (4.59.1)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (1.4.9)
Requirement already satisfied: packaging>=20.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (25.0)
Requirement already satisfied: pillow>=8 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (11.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (3.2.3)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from matplotlib<4.0,>=3.7->kbar==0.1.2) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas<3.0,>=2.0->kbar==0.1.2) (2025.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from pandas<3.0,>=2.0->kbar==0.1.2) (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<4.0,>=3.7->kbar==0.1.2) (1.17.0)
Downloading numpy-1.26.4-cp310-cp310-win_amd64.whl (15.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.8/15.8 MB 1.4 MB/s eta 0:00:00
Installing collected packages: numpy, kbar
  Attempting uninstall: numpy
    Found existing installation: numpy 2.2.6
    Uninstalling numpy-2.2.6:
      Successfully uninstalled numpy-2.2.6
finmind 1.6.9 requires ta~=0.5.25, but you have ta 0.11.0 which is incompatible.
streamlit 1.45.0 requires packaging<25,>=20, but you have packaging 25.0 which is incompatible.
yfinance 0.2.63 requires websockets>=13.0, but you have websockets 11.0.3 which is incompatible.
Successfully installed kbar-0.1.2 numpy-1.26.4

用 pip show 檢視 kbar 套件 :

D:\PyPi\kbar>pip show kbar  
Name: kbar
Version: 0.1.2
Summary: A lightweight K-line (candlestick) plotting tool with matplotlib and mplfinance.
Home-page: https://github.com/tony1966/kbar/
Author: Tony Y.H. Huang
Author-email:
License:
        MIT License

        Copyright (c) [2025] [Tony Y.H.Huang]

        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:

        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.

        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Location: c:\users\tony1\appdata\local\programs\thonny\lib\site-packages
Requires: matplotlib, mplfinance, numpy, pandas, pyarrow
Required-by:

接下來測試 kbar, 因本機環境尚未安裝 yfinance, 故先用 pip 安裝 :

D:\PyPi\kbar>pip install yfinance  

匯入 yfinance 與 kbar 後從 yfinance 下載股票資料

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 yfinance as yf  
>>> from kbar import KBar  
>>> df=yf.download('0050.tw', start='2024-07-01', end='2024-08-21', auto_adjust=False)  
[*********************100%***********************]  1 of 1 completed

新版 yfinance 傳回的 DataFrame 為多層欄位, 用 map() 方法改回單層後才能用 kbar 繪製 K 線圖 (因為 mplfinance 套件只能依據單層 df 繪 K 線圖) :

>>> df.columns=df.columns.map(lambda x: x[0])  
>>> kb=KBar(df)  
>>> kb.plot(volume=True)   




完整的測試程式碼如下 : 

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)

可見安裝的 kbar 套件可正常繪製 K 線圖. 


13. 發佈到 PyPI 測試網站 TestPyPI :

雖然上面在本機成功安裝 kbar 且測試功能正常, 但在正式發佈到 PyPI 之前, 最好還是先發佈到 PyPI 的測試網站, 驗證無問題後再正式發佈至 PyPI 供使用者下載安裝. 

首先到 PyPI 測試網站註冊帳號, 注意, 雖然 TestPyPI 與 PyPI 網站介面長得幾乎一樣, 但它們是兩套獨立的系統, 帳號不共用, 因此需另行註冊 (我都設一樣免得忘記). 


點右上角的 Register 進入註冊頁面 :




填寫姓名, Email, 帳密後按底下的 "Create account" :




這時要去信箱收確認信, 點信中的確認超連結後登入 TestPyPI 才算是 Verified :





接下來要啟用雙因子認證 (2FA) 才能取得發佈套件包的 API token, 登入後按左上角的 "Account settings" :




往下拉到 "Two factors authentication (2FA)", 按左下角的 "Add 2FA with authentication application" 鈕 :




用手機相機掃描 QR 碼, 點擊 URL 後選擇 Google Auth 應用程式, 它會顯示一個 6 位數號碼, 將其填入右方框中, 按 "Verify application" :




驗證成功後 "Add 2FA with authentication application" 鈕就會消失 :




2FA 啟用通過後, 往下移到 "API tokens", 按 "Add API tokens" 鈕 :




輸入一個好記的 Token name, 勾選 Scope 為 "Entire account (all projects)", 然後按底下的 "Create token" 鈕 :




按 "Copy token" 鈕將 API token 複製到記事本儲存備用 :




有了 API token 後就可以用 twine 工具發佈打包好的套件了, 切換到專案目錄 kbar 下, 輸入下列指令, 看到 Enter your API token 提示時, 複製上面取得之 API token 後按 Ctrl+V 貼上, 按 Enter 即開始上傳套件包 : 

python -m twine upload --repository testpypi dist/* 

D:\PyPi\kbar>python -m twine upload --repository testpypi dist/*   
Uploading distributions to https://test.pypi.org/legacy/
Enter your API token:  (按 Ctrl+V 貼上)
Uploading distributions to https://test.pypi.org/legacy/
Uploading kbar-0.1.2-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.1/17.1 kB • 00:00 • ?
Uploading kbar-0.1.2.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.0/19.0 kB • 00:00 • ?

View at:
https://test.pypi.org/project/kbar/0.1.2/

這樣便完成上傳了, 訪問套件網址 :


按右上角的 "Manage project" 鈕會要求輸入登入密碼, 通過才能進入管理頁面 :




這時會顯示兩個版本的 kbar 套件, 其中 v0.1.0 是之前上傳的 Windows 版 kbar, 它在 Colab/Linux 上無法顯示中文; 而目前上傳的 v0.1.2 版則是修正此問題的新版本. 




本篇開頭曾提到, 上傳到 TestPyPI 的專案可以刪除, 例如若要刪除 v0.1.0 版, 只要按右方的 Options 鈕, 選擇 Delete, 輸入版本編號後按 "Delete release" 鈕即可 :




但我傾向先保留 v0.1.0 版. 


14. 在 Colab 從 TestPyPI 安裝 kbar :

上面已順利將 kbar v0.1.2 版套件包發佈到 TestPyPI, 接下來開啟 Colab 輸入下列指令從發佈到 TestPyPI 上的套件安裝 kbar :

!pip install -i https://test.pypi.org/simple/ kbar==0.1.2  

但 TestPyPI 網站裡的 Numpy 版本最新只到 1.9.3 版, 不符合我在 pyproject.toml 裡指定 Numpy 至少需 1.23 版的要求, 因此會出現錯誤 :




只要增加搜尋 PyPI 即可解決此問題 :

!pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple kbar==0.1.2  




安裝好即可輸入下列指令來繪製 K 線圖 :

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)

結果如下 :




可見新版 kbar 確實已解決 Colab 上的中文顯示問題了. 

沒有留言 :