如何將寫好的package上傳到pypi供人安裝使用
1. 準備一份要上傳的python code
架構如下:
在目錄資料夾下須有__init__.py,裡面可為空白也可import檔案。
2. 註冊PyPi帳號
PyPi有提供一個上傳測試用的server TestPypi
PyPi官方提供的測試平台,若想上傳 package 可以在這裡先測試,避免還沒確定好package就已經有n個版本,也可確保釋放出的package正確運行。
PyPi正式平台 : PyPi
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
兩個平台都須先註冊好帳號,上傳package會用到。
3. 套件安裝
詳細解說可以參考
官方文件
設定setup,讓程式知道module裡面的metadata
- name = 套件的名稱,盡量不要包含"-","_"。
- version = 此次發布的版本號
- author = 套件作者
- author_email = 套件作者信箱
- description = 簡短敘述該套件
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
即為黃色標記部分
- long_description = PyPi上對此專案的介紹
- long_description_content_type = 'text/markdown' 讓程式知道這是markdown格式
- url = 此project 的網站,大多會以github為主。
- packages= setuptools.find_packages() → 讓setuptools自己去找進入點
- classifiers = 分類項目 詳細可參考官方文件
- python_requires = '≥3.6' 此專案所需python版本
5. license書寫
這是3rd 需要的文件,以保護自己避免觸法。
簡單的去選擇需要的License:https://choosealicense.com/
請copy and write txt至當前目錄下
6. 執行sdist
程式完成後會產生dist 資料夾,並產生tar.gz。
bdist_wheel這個參數建立了wheelfile於dist資料夾下
7. 上傳打包好的tar.gz至PyPi test上
建議每次手動輸入帳號密碼驗證(官方推薦),也可以create一個.pypirc file來放帳號密碼,但安全性方面較不足
twine套件會把dist/*上傳至伺服器,沒有error出現的話就可以去PyPi test網站上檢查是否已經上傳
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
8. 測試
照著網站上的指令安裝測試伺服器上的package
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
9. 測試成功後,正式上傳至PyPi上
到PyPi官網看到自己的專案就是有上傳成功囉。
參考網站