這篇文章主要介紹了如何開發自己的 Python 套件,並將其上傳到 PyPI (Python Package Index),以便其他開發者可以輕鬆地 pip install
使用。文章中會先介紹前置準備工作,包括為專案建立 LICENSE 和 setup.py、並安裝 setuptools 和安裝 twine 工具以及建立 PyPI 帳號等,最後就能將專案上傳到 PyPI 囉!
Table
1. Package 資料結構
首先介紹一下 package 的資料夾的結構,我們會新增 setup.py 和 LICENSE,然後package_name 裡面會放寫好要打包的 module。
1 2 3 4 5 6 7 8 |
folder |-- setup.py |-- LICENSE |-- README.md |-- package_name |-- __init__.py |-- module1.py |-- module2.py |
2. 新增 Open Source License
MIT、Apache 2.0 以及 GPLv3 都是常見的 open soucre license.
- 我們這邊選擇 MIT License
- 最多人使用,且限制少的 License
- 任何人可以複製、修改和商業使用
- 僅限制需要註明著作權聲明 (Copyright) 和 MIT license
- 建立一個 LICENSE 檔案如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
MIT License Copyright (c) 2018-2022, HsuanChi Su All rights reserved. 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. |
3. 新增 setup.py
要修改的變數名稱如下:
- PACKAGE_NAME: package 名稱
- AUTHOR: 作者
- AUTHOR_EMAIL: 作者信箱
- URL: 網址,可以放 Github 連結
- DOWNLOAD_URL: 下載網址,可以放 pypi 連結
- LICENSE: 選擇使用的 LICENSE
- VERSION: 版本號,每次更新需要修改
- DESCRIPTION: 描述,可以直接引用 README.md
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 |
import pathlib from setuptools import setup, find_packages HERE = pathlib.Path(__file__).parent.resolve() PACKAGE_NAME = "Find_Sitemap" AUTHOR = "HsuanChi Su" URL = "https://github.com/hsuanchi/Find-Sitemaps" DOWNLOAD_URL = "https://pypi.org/project/Find-sitemap/" LICENSE = "MIT" VERSION = "1.0.0" DESCRIPTION = "A set of data tools in Python" LONG_DESCRIPTION = (HERE / "docs" / "README.md").read_text(encoding="utf8") LONG_DESC_TYPE = "text/markdown" requirements = (HERE / "requirements.txt").read_text(encoding="utf8") INSTALL_REQUIRES = [s.strip() for s in requirements.split("\n")] dev_requirements = (HERE / "dev_requirements.txt").read_text(encoding="utf8") EXTRAS_REQUIRE = {"dev": [s.strip() for s in dev_requirements.split("\n")]} CLASSIFIERS = [f"Programming Language :: Python :: 3.{str(v)}" for v in range(7, 12)] PYTHON_REQUIRES = ">=3.7" setup( name=PACKAGE_NAME, version=VERSION, description=DESCRIPTION, long_description=LONG_DESCRIPTION, long_description_content_type=LONG_DESC_TYPE, author=AUTHOR, license=LICENSE, author_email=AUTHOR_EMAIL, url=URL, download_url=DOWNLOAD_URL, python_requires=PYTHON_REQUIRES, install_requires=INSTALL_REQUIRES, extras_require=EXTRAS_REQUIRE, packages=find_packages(), classifiers=CLASSIFIERS, ) |
4. 建立 PyPI 帳號
- PyPI (Python Package Index) 是 Python 正式的第三方 package 線上儲存庫,需要到正式站和測試站都各辦一個 pypi 帳號,兩者的帳號是不通用的
- https://pypi.org/
- https://test.pypi.org/
5. 上傳 package!
- 再來安裝 twine 和 setuptools 套件,setuptools 是用來打包我們的 package 而 twine 是用來上傳我們寫好的 packeage 到 PyPI
1 |
$ pip install twine setuptools |
- 上傳到 TestPyPI,會需要輸入帳號密碼,或你也可以使用 API Token 方式驗證
1 2 |
python setup.py sdist bdist_wheel twine upload --repository testpypi --skip-existing dist/* |
- 上傳到正式 PyPI
1 2 |
python setup.py sdist bdist_wheel twine upload --skip-existing dist/* |
上傳完成後,就可以到 https://pypi.org/project/{package neame}/,看到自己的 python package 囉!