目錄

Python 模組製作筆記

封面圖片為 ChatGPT 生成,作者編輯。

近期在學習如何將撰寫好的 Python 進行打包,並進行快速分享與安裝使用。在研究過各種分享渠道後,為了方便使用者(當然是我懶得一再安裝),顯然,製作成模組或許是最方便的方式之一。

以下以文字方式,記錄我的製作過程。

撰寫模組

說到打包模組的前置準備,那當然需要準備一個已經寫好的模組了。最近在嘗試將 Wen-Ting Wang 所製作的基於 Sheng-Li Tzeng 與 Hsin-Cheng Huang 論文 Resolution Adaptive Fixed Rank Kriging 的空間插值工具 autoFRK ,從 R 語言移植到 Python 中,用以在基於 PyTorch 的深度學習環境中調用該模型。

該模組中程式碼的部分內容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# import modules
import torch
import torch.nn as nn
from typing import Optional, Union
from autoFRK.utils.logger import setup_logger
from autoFRK.utils.device import setup_device
from autoFRK.utils.utils import *
from autoFRK.utils.predictor import *

# logger config
LOGGER = setup_logger()

# class AutoFRK
class AutoFRK(nn.Module):
    """
    Automatic Fixed Rank Kriging

    This function performs resolution-adaptive fixed rank kriging on spatial
    data observed at one or multiple time points using a spatial random-effects
    model.

    ...
    """
    def __init__(
        self,
        mu: Union[float, torch.Tensor]=0.0, 
        D: torch.Tensor=None, 
        G: torch.Tensor=None,
        finescale: bool=False, 
        maxit: int=50, 
        tolerance: float=1e-6,
        maxK: int=None, 
        Kseq: torch.Tensor=None, 
        method: str="fast", 
        n_neighbor: int=3, 
        maxknot: int=5000,
        dtype: torch.dtype=torch.float64,
        device: Optional[Union[torch.device, str]]=None
        ):
        """
        Initialize autoFRK model with tensor-safe and device-aware configuration.
        """
        super().__init__()
        
        ...

前置準備

在打包模組前,我們需要整理模組檔案,其目錄下:

此模組的專案目錄。

其中, 專案目錄/src/模組名稱/ 中放置所有模組的原始程式碼。每個子目錄都需包含 __init__.py 檔案,用以宣告該目錄為 Python 模組,使 Python 能夠正確匯入其內容。

在專案的根目錄下,通常還會包含一些輔助檔案與資料夾,例如 tests/ 用於放置單元測試或整合測試程式; pyproject.toml 用於定義專案的基本資訊、依賴套件與建置方式; README.md 作為專案的說明文件; LICENSE 為模組授權條款;而 setup.cfgsetup.py 則用於相容舊式的打包方式,並非是必要的。此外, MANIFEST.in 用來指定在打包時需額外包含的非程式檔案,例如文件、資料檔或靜態資源。這些檔案構成模組發佈所需的結構,使專案在開發、測試與打包階段都能保持明確且一致的架構。

以下操作皆位於 Windows 11 操作系統。

在此模組中, pyproject.tomlMANIFEST.in 如下:

pyproject.toml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "模組名稱"
version = "模組版本"
description = "模組描述"
readme = "README.md"
requires-python = ">=3.10"
license = { file = "LICENSE" }
authors = [
  { name="作者名稱", email="電子信箱" }
]

dependencies = [  # 依賴的前置模組
  "numpy>=1.23",
  "torch>=2.0",
  "scipy>=1.10",
  "faiss-cpu>=1.7.4",
  "scikit-learn>=1.3",
  "colorlog>=6.7",
]

[project.optional-dependencies]
gpu = [  # GPU 版本依賴的前置模組
  "faiss-gpu>=1.7.4"
]

[project.urls]
Homepage = "Github 專案位置"
Issues = "Github 專案 issues 位置"

[tool.setuptools.packages.find]
where = ["src"]
include = ["模組名稱*"]

MANIFEST.in

1
2
3
include README.md
include LICENSE
recursive-include src/模組名稱 *.py *.txt *.md

打包模組

在進行打包前,首先要安裝打包工具。在終端輸入以下指令安裝 build 工具。

1
pip install --upgrade build

build 是 Python 官方推薦的打包工具,會進行以下操作。

  • 讀取 pyproject.toml 中的設定。
  • 使用 setuptools 自動尋找模組(find_packages)。
  • 將原始碼目錄(如 專案目錄/src/)轉換為可發佈的封裝檔案。

執行打包後,系統會在 dist/ 目錄下生成兩種常見的發佈檔案:

  • .whl Wheel 格式的二進位封裝檔(binary distribution)。
  • .tar.gz 包含原始碼的壓縮封裝檔(source distribution)。

build 安裝完成後,於專案根目錄開啟終端,並執行以下指令,即可生成封裝檔案:

1
python -m build

此操作會在專案根目錄生成 dist/ 資料夾,內容如下:

1
2
3
dist/
├── 模組名稱-模組版本-py3-none-any.whl
└── 模組名稱-模組版本.tar.gz

這些檔案即可用於安裝、發佈或上傳至 Python 官方套件庫 PyPI。

測試模組

在將製作好的模組分享給其他人使用前,我們應該先進行測試,確保安裝與運作是否正常。

我們可以在專案根目錄執行以下指令,使用 editable 模式,讓我們在修改原始碼後,無需重新打包就能夠進行測試。

1
pip install -e .

若該模組提供 GPU 相關功能,可使用下列指令安裝可選依賴。

1
pip install -e .[gpu]
執行結果參考
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Microsoft Windows [版本 10.0.26100.6584]
(c) Microsoft Corporation. 著作權所有,並保留一切權利。

C:\Users\user\Desktop\github\autoFRK-python\autoFRK>python -m build
* Creating isolated environment: venv+pip...
* Installing packages in isolated environment:
  - setuptools>=61.0
  - wheel
* Getting build dependencies for sdist...
C:\Users\user\AppData\Local\Temp\build-env-vieuwf3x\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)
running egg_info
creating src\autoFRK.egg-info
writing src\autoFRK.egg-info\PKG-INFO
writing dependency_links to src\autoFRK.egg-info\dependency_links.txt
writing requirements to src\autoFRK.egg-info\requires.txt
writing top-level names to src\autoFRK.egg-info\top_level.txt
writing manifest file 'src\autoFRK.egg-info\SOURCES.txt'
reading manifest file 'src\autoFRK.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.txt' under directory 'src\autoFRK'
warning: no files found matching '*.md' under directory 'src\autoFRK'
adding license file 'LICENSE'
writing manifest file 'src\autoFRK.egg-info\SOURCES.txt'
* Building sdist...
C:\Users\user\AppData\Local\Temp\build-env-vieuwf3x\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)
running sdist
running egg_info
writing src\autoFRK.egg-info\PKG-INFO
writing dependency_links to src\autoFRK.egg-info\dependency_links.txt
writing requirements to src\autoFRK.egg-info\requires.txt
writing top-level names to src\autoFRK.egg-info\top_level.txt
reading manifest file 'src\autoFRK.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.txt' under directory 'src\autoFRK'
warning: no files found matching '*.md' under directory 'src\autoFRK'
adding license file 'LICENSE'
writing manifest file 'src\autoFRK.egg-info\SOURCES.txt'
running check
creating autofrk-0.1.0
creating autofrk-0.1.0\src\autoFRK
creating autofrk-0.1.0\src\autoFRK.egg-info
creating autofrk-0.1.0\src\autoFRK\utils
copying files to autofrk-0.1.0...
copying LICENSE -> autofrk-0.1.0
copying MANIFEST.in -> autofrk-0.1.0
copying README.md -> autofrk-0.1.0
copying pyproject.toml -> autofrk-0.1.0
copying src\autoFRK\___init__.py -> autofrk-0.1.0\src\autoFRK
copying src\autoFRK\autoFRK.py -> autofrk-0.1.0\src\autoFRK
copying src\autoFRK\mrts.py -> autofrk-0.1.0\src\autoFRK
copying src\autoFRK.egg-info\PKG-INFO -> autofrk-0.1.0\src\autoFRK.egg-info
copying src\autoFRK.egg-info\SOURCES.txt -> autofrk-0.1.0\src\autoFRK.egg-info
copying src\autoFRK.egg-info\dependency_links.txt -> autofrk-0.1.0\src\autoFRK.egg-info
copying src\autoFRK.egg-info\requires.txt -> autofrk-0.1.0\src\autoFRK.egg-info
copying src\autoFRK.egg-info\top_level.txt -> autofrk-0.1.0\src\autoFRK.egg-info
copying src\autoFRK\utils\___init__.py -> autofrk-0.1.0\src\autoFRK\utils
copying src\autoFRK\utils\device.py -> autofrk-0.1.0\src\autoFRK\utils
copying src\autoFRK\utils\logger.py -> autofrk-0.1.0\src\autoFRK\utils
copying src\autoFRK\utils\predictor.py -> autofrk-0.1.0\src\autoFRK\utils
copying src\autoFRK\utils\utils.py -> autofrk-0.1.0\src\autoFRK\utils
copying src\autoFRK.egg-info\SOURCES.txt -> autofrk-0.1.0\src\autoFRK.egg-info
Writing autofrk-0.1.0\setup.cfg
Creating tar archive
removing 'autofrk-0.1.0' (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\user\AppData\Local\Temp\build-env-exul05dl\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)
running egg_info
writing src\autoFRK.egg-info\PKG-INFO
writing dependency_links to src\autoFRK.egg-info\dependency_links.txt
writing requirements to src\autoFRK.egg-info\requires.txt
writing top-level names to src\autoFRK.egg-info\top_level.txt
reading manifest file 'src\autoFRK.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.txt' under directory 'src\autoFRK'
warning: no files found matching '*.md' under directory 'src\autoFRK'
adding license file 'LICENSE'
writing manifest file 'src\autoFRK.egg-info\SOURCES.txt'
* Building wheel...
C:\Users\user\AppData\Local\Temp\build-env-exul05dl\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)
running bdist_wheel
running build
running build_py
creating build\lib\autoFRK
copying src\autoFRK\autoFRK.py -> build\lib\autoFRK
copying src\autoFRK\mrts.py -> build\lib\autoFRK
copying src\autoFRK\___init__.py -> build\lib\autoFRK
creating build\lib\autoFRK\utils
copying src\autoFRK\utils\device.py -> build\lib\autoFRK\utils
copying src\autoFRK\utils\logger.py -> build\lib\autoFRK\utils
copying src\autoFRK\utils\predictor.py -> build\lib\autoFRK\utils
copying src\autoFRK\utils\utils.py -> build\lib\autoFRK\utils
copying src\autoFRK\utils\___init__.py -> build\lib\autoFRK\utils
running egg_info
writing src\autoFRK.egg-info\PKG-INFO
writing dependency_links to src\autoFRK.egg-info\dependency_links.txt
writing requirements to src\autoFRK.egg-info\requires.txt
writing top-level names to src\autoFRK.egg-info\top_level.txt
reading manifest file 'src\autoFRK.egg-info\SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.txt' under directory 'src\autoFRK'
warning: no files found matching '*.md' under directory 'src\autoFRK'
adding license file 'LICENSE'
writing manifest file 'src\autoFRK.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\autoFRK
copying build\lib\autoFRK\autoFRK.py -> build\bdist.win-amd64\wheel\.\autoFRK
copying build\lib\autoFRK\mrts.py -> build\bdist.win-amd64\wheel\.\autoFRK
creating build\bdist.win-amd64\wheel\autoFRK\utils
copying build\lib\autoFRK\utils\device.py -> build\bdist.win-amd64\wheel\.\autoFRK\utils
copying build\lib\autoFRK\utils\logger.py -> build\bdist.win-amd64\wheel\.\autoFRK\utils
copying build\lib\autoFRK\utils\predictor.py -> build\bdist.win-amd64\wheel\.\autoFRK\utils
copying build\lib\autoFRK\utils\utils.py -> build\bdist.win-amd64\wheel\.\autoFRK\utils
copying build\lib\autoFRK\utils\___init__.py -> build\bdist.win-amd64\wheel\.\autoFRK\utils
copying build\lib\autoFRK\___init__.py -> build\bdist.win-amd64\wheel\.\autoFRK
running install_egg_info
Copying src\autoFRK.egg-info to build\bdist.win-amd64\wheel\.\autoFRK-0.1.0-py3.12.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\autofrk-0.1.0.dist-info\WHEEL
creating 'C:\Users\user\Desktop\github\autoFRK-python\autoFRK\dist\.tmp-mxuu22cz\autofrk-0.1.0-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'autoFRK/___init__.py'
adding 'autoFRK/autoFRK.py'
adding 'autoFRK/mrts.py'
adding 'autoFRK/utils/___init__.py'
adding 'autoFRK/utils/device.py'
adding 'autoFRK/utils/logger.py'
adding 'autoFRK/utils/predictor.py'
adding 'autoFRK/utils/utils.py'
adding 'autofrk-0.1.0.dist-info/licenses/LICENSE'
adding 'autofrk-0.1.0.dist-info/METADATA'
adding 'autofrk-0.1.0.dist-info/WHEEL'
adding 'autofrk-0.1.0.dist-info/top_level.txt'
adding 'autofrk-0.1.0.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built autofrk-0.1.0.tar.gz and autofrk-0.1.0-py3-none-any.whl

C:\Users\user\Desktop\github\autoFRK-python\autoFRK>

上傳模組

我們可以透過 twine 工具將剛才建立的封裝檔案上傳到 PyPI(Python Package Index),讓其他人能透過 pip install 指令安裝使用。

PyPI 是 pip 預設的模組來源,我們可以從 PyPI 下載並安裝模組。

1
pip install 模組名稱

首先,安裝上傳工具 twine

1
pip install --upgrade twine

TestPyPI

PyPI 提供測試上傳的環境,我們可以選擇在 TestPyPI 先進行測試,再上傳到 PyPI 。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/1.png
TestPyPI 帳號註冊頁面。

如果是首次使用,註冊 TestPyPI 帳號後,要到電子信箱中收取驗證信。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/2.png
確認帳號。

若是首次使用,則返回 TestPyPI 時要先儲存恢復代碼(recovery code),用於在帳號遺失時登入。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/3.png
輸入一組儲存的恢復代碼以確認。

接下來必須啟用 2FA 驗證,才能進行其他操作。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/4.png
2FA 驗證。

現在,可以在 Account settings 中的 API tokens 生成一個 token ,讓我們可以從終端上傳模組。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/5.png
管理 token 。
https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/6.png
建立 token 。

建立的 token 僅會顯示一次。獲取 token 後,創建 C:\Users\使用者名稱\.pypirc 檔案,並輸入以下內容儲存 token 。

1
2
3
[testpypi]
  username = __token__
  password = 你的 token

接下來,重新在專案目錄開啟終端,並輸入以下指令上傳模組至 TestPyPI 。

1
twine upload --repository testpypi dist/*

其執行結果應如下

執行結果參考
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
PS C:\Users\user\Desktop\github\autoFRK-python> twine upload --repository testpypi dist/*
>> 
Uploading distributions to https://test.pypi.org/legacy/
Uploading autofrk-0.1.0-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.8/100.8 kB • 00:00 • 1.1 MB/s        
Uploading autofrk-0.1.0.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 110.3/110.3 kB • 00:00 • ?

View at:
https://test.pypi.org/project/autoFRK/0.1.0/

回到 TestPyPI 後,就可以在 Your projects 區塊看到剛上傳的模組了。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/TestPyPI/7.png
剛上傳的模組 。

確認上傳後,即可使用以下指令測試安裝。其中,使用 --extra-index-url https://pypi.org/simple 以確保從正式環境中找到需要的前置模組。

1
pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple 模組名稱

PyPI

等待一切都準備完畢後,就可以正式上傳到 PyPI 了。以下記錄詳細的上傳流程。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/1.png
PyPI 帳號註冊頁面。

首先,若是初次使用 PyPI ,則需要先建立帳號。依照帳號註冊流程後,與 TestPyPI 帳號註冊一樣,至電子信箱中收取驗證信。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/2.png
確認帳號。

返回 PyPI 後,同樣先儲存恢復代碼。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/3.png
儲存恢復代碼。

輸入密碼後繼續。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/4.png
輸入密碼。

儲存恢復代碼,建議點擊 Save 儲存到本地電腦。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/5.png
儲存恢復代碼。

接下來,輸入一組儲存的恢復代碼以確認帳號。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/6.png
輸入一組儲存的恢復代碼以確認。

與 TestPyPI 同樣地,必須啟用 2FA 驗證,才能進行其他操作。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/7.png
啟用 2FA 驗證。

至此,帳號註冊完畢。

現在,與在 TestPyPI 的流程相同,要先建立一個 token ,才能從本機上傳模組。

首先,進入 Account settings

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/8.png
Account settings 。

找到 API tokens 區域,並點擊 Add API token

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/9.png
API tokens 。

填寫 token 的名稱,方便往後辨識。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/10.png
Tokens name 。

Scope 選擇此 token 的權限(應該只能選擇 Entire account)並點擊 Create token 建立 token 。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/11.png
選擇權限並建立 token 。

複製下方紅框處的文字,稍後會用到。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/12.png
PyPI token 。

在本機路徑 C:\Users\使用者名稱\ 中新增 .pypirc 檔案,並輸入以下內容儲存剛才的 token 。如果先前使用過 TestPyPI ,其 token 也會儲存於此。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/13.png
儲存 PyPI token 。

接下來,於專案目錄開啟終端,並輸入以下指令上傳模組至 PyPI 。

1
twine upload dist/*

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/14.png
上傳模組。

其執行結果應如下

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/15.png
上傳成功。

提醒
PyPI 和 TestPyPI 不接受相同上傳版本號的模組。每次上傳時,記得更新 pyproject.toml 中的模組版本。

回到 PyPI 後,就可以在 Your projects 區塊看到剛上傳的模組了。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/16.png
剛上傳的模組 。

我們也可以點擊旁邊的 View ,查看模組頁面。其內容會顯示先前所寫的 README 內容。

https://raw.githubusercontent.com/Josh-test-lab/website-assets-repository/refs/heads/main/posts/Notes%20on%20Creating%20Python%20Modules/PyPI/17.png
模組頁面 。

確認上傳後,即可使用以下指令安裝模組。

1
pip install 模組名稱

結語

自製 Python 模組是一件令人興奮的事,可以分享自己建立的模組讓我感到非常有成就感。在建立本次模組時,我也進行了大量的測試,同時也認識到將 README 檔案完善與有序地整理模組檔案是件重要的事。希望未來能將此模組的相關內容完善,持續優化功能效能與使用介面,讓它在實際應用上發揮更大的效益。

運行環境

  • 作業系統:Windows 11 24H2
  • 程式語言:Python 3.12.8

參見

注意
本文最後更新時間為 2025年10月22日,文中內容可能已經過時,敬請留意。若出現內容錯誤、圖片失效等問題,歡迎留言指正。