Try   HackMD

如何將寫好的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
# 如何看package目錄結構 (linux環境下) apt install tree # 在該資料夾下執行 tree

在目錄資料夾下須有__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. 套件安裝

!pip install twine
!pip install wheel

詳細解說可以參考 官方文件

4. 設定setup.py

設定setup,讓程式知道module裡面的metadata

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 = 簡短敘述該套件
    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

python setup.py

程式完成後會產生dist 資料夾,並產生tar.gz。

python setup.py sdist bdist_wheel 

bdist_wheel這個參數建立了wheelfile於dist資料夾下

7. 上傳打包好的tar.gz至PyPi test上

建議每次手動輸入帳號密碼驗證(官方推薦),也可以create一個.pypirc file來放帳號密碼,但安全性方面較不足

python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

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上

python -m twine upload dist/*

PyPi官網看到自己的專案就是有上傳成功囉。

參考網站