Python程式包的構建和釋出過程
關於我
一個有思想的程式猿,終身學習實踐者,目前在一個創業團隊任team lead,技術棧涉及Android、Python、Java和Go,這個也是我們團隊的主要技術棧。
Github:https://github.com/hylinux1024
微信公眾號:終身開發者(angrycode)
當我們開發了一個開源專案時,就希望把這個專案打包然後釋出到pypi.org
上,別人就可以通過pip install
的命令進行安裝。本文的教程來自於Python
官方文件,如有不正確的地方歡迎評論拍磚。
0x00 建立專案
本文使用到的專案目錄為
➜ packaging-tutorial . └── bestpkg └── __init__.py
接下來的所有操作都是在packing_tutorial
這個目錄下進行的。首先把bestpkg
這個目錄下的__init__.py
新增以下內容
info='packaging demo'
這個資訊主要用於打包成功後安裝測試用的。
0x01 專案結構
一個待發布的專案還需要有以下這些檔案:setup.py
、LICENSE
和README.md
➜ packaging-tutorial
.
├── LICENSE
├── README.md
├── bestpkg
│ └── __init__.py
└── setup.py
0x02 setup.py
setup.py
檔案是給setuptools
setuptools
如何構建我們的專案。開啟編輯器,編輯setup.py
檔案,輸入以下內容
import setuptools # 讀取專案的readme介紹 with open("README.md", "r") as fh: long_description = fh.read() setuptools.setup( name="bestpkg",# 專案名稱,保證它的唯一性,不要跟已存在的包名衝突即可 version="0.0.1", author="hylinux1024", # 專案作者 author_email="[email protected]", description="一個牛逼的程式", # 專案的一句話描述 long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/hylinux1024/niubiproject",# 專案地址 packages=setuptools.find_packages(), classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], )
- name
專案名稱,保證它的唯一性,不要跟已存在的包名衝突即可,否則會發布失敗 - version
版本號
- author
作者 - author_email
作者郵箱 - description
一句話描述專案 - long_description
專案詳細說明,一般直接讀取README.md的內容 - url
專案的連結地址 - packages
列出當前專案的包,一般直接使用find_packages()
即可 - classifiers
這裡指定Python
的相容版本是Python3
,也指定了專案使用的開源協議。
0x03 README.md
給專案新增詳細的README
# Example Package
This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.
0x04 LICENSE
要釋出包到pypi
上,選擇一個合適的開源協議是非常重要的。如果不知道怎麼選可以到https://choosealicense.com/這裡看看。
0x05 專案打包
專案需要打包後才能釋出,要打包專案需先安裝最新版本的setuptools
和wheel
➜ python3 -m pip install --user --upgrade setuptools wheel
然後使用以下命令進行打包
➜ python3 setup.py sdist bdist_wheel
當看到以下資訊,說明已經打包成功
...
...
...
adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
creating build/bdist.macosx-10.14-x86_64/wheel/bestpkg-0.0.1.dist-info/WHEEL
creating 'dist/bestpkg-0.0.1-py3-none-any.whl' and adding 'build/bdist.macosx-10.14-x86_64/wheel' to it
adding 'bestpkg/__init__.py'
adding 'bestpkg-0.0.1.dist-info/LICENSE'
adding 'bestpkg-0.0.1.dist-info/METADATA'
adding 'bestpkg-0.0.1.dist-info/WHEEL'
adding 'bestpkg-0.0.1.dist-info/top_level.txt'
adding 'bestpkg-0.0.1.dist-info/RECORD'
removing build/bdist.macosx-10.14-x86_64/wheel
在專案目錄下會生成一個dist
和build
資料夾
➜ packaging-tutorial tree
.
├── LICENSE
├── README.md
├── bestpkg
│ └── __init__.py
├── bestpkg.egg-info
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ ├── dependency_links.txt
│ └── top_level.txt
├── build
│ ├── bdist.macosx-10.14-x86_64
│ ├── bdist.macosx-10.9-x86_64
│ └── lib
│ └── bestpkg
│ └── __init__.py
├── dist
│ ├── bestpkg-0.0.1-py3-none-any.whl
│ └── bestpkg-0.0.1.tar.gz
└── setup.py
8 directories, 11 files
在dist
檔案中有兩個檔案
dist
├── bestpkg-0.0.1-py3-none-any.whl
└── bestpkg-0.0.1.tar.gz
tar.gz
檔案是原始碼檔案壓縮包,而.whl
就是打包後的檔案。最新的pip
命令會安裝這個.whl
檔案。
0x06 上傳
現在就可以上傳到Python
索引庫了。我們使用Test PyPI
,這個是測試用的Pypi
,本例子也是使用Test Pypi
。
首先要到https://test.pypi.org/account/register/註冊賬號。本例中我註冊的賬號為:hylinux1024。
然後使用twine
工具來上傳我們的包。使用以下命令進行安裝:
➜ python3 -m pip install --user --upgrade twine
使用以下命令上傳dist
目錄下的檔案
➜ python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
這個命令會提示輸入剛在test.pypi.org
上註冊賬號密碼,並出現類似以下資訊後說明已經上傳成功。
Enter your username: hylinux1024
Enter your password:
Uploading distributions to https://test.pypi.org/legacy/
Uploading bestpkg-0.0.1-py3-none-any.whl
100%|██████████████████████████████████████| 4.57k/4.57k [00:00<00:00, 8.01kB/s]
Uploading bestpkg-0.0.1.tar.gz
100%|██████████████████████████████████████| 4.18k/4.18k [00:01<00:00, 3.23kB/s]
然後開啟https://test.pypi.org/project/bestpkg/這個地址就可以看到我們釋出的包。
0x07 安裝
釋出成功之後就可以使用pip
來安裝來。我們在虛擬環境中安裝,關於虛擬環境可以看我前一篇文章。
這裡就使用pipenv
,這裡我直接進入到我昨天建立的那個專案中,也為了更好演示安裝結果。
➜ pip install --index-url https://test.pypi.org/simple/ --no-deps bestpkg
在這裡我使用--index-url
引數是為了指定從test.pypi.org
中安裝,而不是正式包索引庫中查詢要安裝的包。還有使用了--no-deps
引數是因為本例中沒有使用到其它的依賴庫。
在終端會看到以下類似資訊,說明安裝成功
Looking in indexes: https://test.pypi.org/simple/
Collecting bestpkg
Downloading https://test-files.pythonhosted.org/packages/5a/fc/c109b3872b6c06e7368c30b6e52501113846f90ca716a434766151093173/bestpkg-0.0.1-py3-none-any.whl
Installing collected packages: bestpkg
Successfully installed bestpkg-0.0.1
進入互動介面
(pipenvdemo) ➜ pipenvdemo python
>>> import bestpkg
>>> bestpkg.info
'packaging demo'
info
變數就是在__init__.py
檔案中定義的變數。自此我們的包釋出、安裝使用流程就走完了。
要在正式的Python
索引庫中釋出,只需要到https://pypi.org/註冊賬號,並上傳就可以了。
0x08 總結一下
通過一個簡單的例子展示Python
通過setuptools
工具進行打包,然後上傳到test.pypi.org
的流程。如果要上傳到正式的pypi.org
上,只需要註冊一個正式的賬號。一旦釋出成功就可以使用pip install [your-package]
的命令進行安裝。
0x09 引用
- https://packaging.python.org/tutorials/packaging-projects/
Packaging Python Projects