# 如何將寫好的package上傳到pypi供人安裝使用 - [ ] 1. 準備一份要上傳的python code - [ ] 2. 註冊PyPi帳號 - [ ] 3. 套件安裝 - [ ] 4. 設定setup.py - [ ] 5. license書寫 - [ ] 6. 執行sdist - [ ] 7. 上傳打包好的tar.gz至PyPi test上 - [ ] 8. 測試 - [ ] 9. 測試成功後,正式上傳至PyPi上 ## 1. 準備一份要上傳的python code 架構如下: ``` |-- VerdictCut |-- VerdictCut | |-- __init__.py | |-- find_fact.py | |-- find_justice.py | |-- find_laws.py | |-- find_maintext.py | |-- find_roles.py ``` ```python= # 如何看package目錄結構 (linux環境下) apt install tree # 在該資料夾下執行 tree ``` 在目錄資料夾下須有__init__.py,裡面可為空白也可import檔案。 ## 2. 註冊PyPi帳號 PyPi有提供一個上傳測試用的server [TestPypi](https://test.pypi.org/) PyPi官方提供的測試平台,若想上傳 package 可以在這裡先測試,避免還沒確定好package就已經有n個版本,也可確保釋放出的package正確運行。 ### PyPi正式平台 : [PyPi](https://pypi.org/) ![](https://i.imgur.com/wdxlNbh.jpg) 兩個平台都須先註冊好帳號,上傳package會用到。 ## 3. 套件安裝 ``` !pip install twine !pip install wheel ``` 詳細解說可以參考 [官方文件](https://packaging.python.org/guides/distributing-packages-using-setuptools/) ## 4. 設定setup.py 設定setup,讓程式知道module裡面的metadata ```python import setuptools # 若Discription.md中有中文 須加上 encoding="utf-8" with open("Discription.md", "r",encoding="utf-8") as f: long_description = f.read() setuptools.setup( name = "VerdictCut", version = "0.1.1", author = "seanbbear", author_email="k7489759@gmail.com", description="cut the verdict into different part", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/seanbbear/VerdictCut", packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], python_requires='>=3.6' ) ``` * name = 套件的名稱,盡量不要包含"-","_"。 * version = 此次發布的版本號 * author = 套件作者 * author_email = 套件作者信箱 * description = 簡短敘述該套件 ![](https://i.imgur.com/fkHE6kn.png) 即為黃色標記部分 * long_description = PyPi上對此專案的介紹 * long_description_content_type = 'text/markdown' 讓程式知道這是markdown格式 * url = 此project 的網站,大多會以github為主。 * packages= setuptools.find_packages() → 讓setuptools自己去找進入點 * classifiers = 分類項目 詳細可參考[官方文件](https://pypi.org/classifiers/) * python_requires = '≥3.6' 此專案所需python版本 ## 5. license書寫 這是3rd 需要的文件,以保護自己避免觸法。 簡單的去選擇需要的License:[https://choosealicense.com/](https://choosealicense.com/) 請copy and write txt至當前目錄下 ## 6. 執行sdist ```python python setup.py ``` 程式完成後會產生dist 資料夾,並產生tar.gz。 ```python python setup.py sdist bdist_wheel ``` bdist_wheel這個參數建立了wheelfile於dist資料夾下 ## 7. 上傳打包好的tar.gz至PyPi test上 建議每次手動輸入帳號密碼驗證(官方推薦),也可以create一個.pypirc file來放帳號密碼,但安全性方面較不足 ```python python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* ``` twine套件會把dist/*上傳至[伺服器](https://test.pypi.org/legacy/),沒有error出現的話就可以去[PyPi test](https://test.pypi.org/)網站上檢查是否已經上傳 ![](https://i.imgur.com/u3EvRbN.png) ## 8. 測試 照著網站上的指令安裝測試伺服器上的package ![](https://i.imgur.com/E71wVGI.png) ## 9. 測試成功後,正式上傳至PyPi上 ```python python -m twine upload dist/* ``` 到[PyPi官網](https://pypi.org/)看到自己的專案就是有上傳成功囉。 [參考網站](https://medium.com/%E8%B3%87%E5%B7%A5%E7%AD%86%E8%A8%98/%E6%89%93%E5%8C%85python-module-%E5%88%B0pypi-%E4%B8%8A-aef1f73e1774)