關于我
編程界的一名小程序猿,目前在一個創業團隊任team lead,技術棧涉及Android、Python、Java和Go,這個也是我們團隊的主要技術棧。 聯系:hylinux1024@gmail.com
當我們開發了一個開源項目時,就希望把這個項目打包然后發布到 pypi.org 上,別人就可以通過 pip install 的命令進行安裝。本文的教程來自于 Python 官方文檔 , 如有不正確的地方歡迎評論拍磚。
0x00 創建項目
本文使用到的項目目錄為
1
2
3
4
|
? packaging - tutorial . └── bestpkg └── __init__.py |
接下來的所有操作都是在 packing_tutorial 這個目錄下進行的。首先把 bestpkg 這個目錄下的 __init__.py 添加以下內容
info='packaging demo'
這個信息主要用于打包成功后安裝測試用的。
0x01 項目結構
一個待發布的項目還需要有以下這些文件: setup.py 、 LICENSE 和 README.md
1
2
3
4
5
6
7
|
? packaging - tutorial . ├── LICENSE ├── README.md ├── bestpkg │ └── __init__.py └── setup.py |
0x02 setup.py
setup.py 文件是給 setuptools 工具的使用腳本,告訴 setuptools 如何構建我們的項目。打開編輯器,編輯 setup.py 文件,輸入以下內容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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 = "hylinux1024@gmail.com" , 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
1
2
3
4
5
|
# 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 上,選擇一個合適的開源協議是非常重要的。如果不知道怎么選可以到choosealicense.com/這里看看。
0x05 項目打包
項目需要打包后才能發布,要打包項目需先安裝最新版本的 setuptools 和 wheel
? python3 -m pip install --user --upgrade setuptools wheel
然后使用以下命令進行打包
? python3 setup.py sdist bdist_wheel
當看到以下信息,說明已經打包成功
1
2
3
4
5
6
7
8
9
10
11
12
13
|
... ... ... 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 文件夾
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
? 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 文件中有兩個文件
1
2
3
|
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 。
首先要到 test.pypi.org/account/reg… 注冊賬號。本例中我注冊的賬號為: 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 上注冊賬號密碼,并出現類似以下信息后說明已經上傳成功。
1
2
3
4
5
6
7
|
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] |
然后打開 test.pypi.org/project/bes… 這個地址就可以看到我們發布的包。
0x07 安裝
發布成功之后就可以使用 pip 來安裝來。我們在虛擬環境中安裝,關于虛擬環境可以看我前一篇文章。
這里就使用 pipenv ,這里我直接進入到我昨天創建的那個項目中,也為了更好演示安裝結果。
? pip install --index-url https://test.pypi.org/simple/ --no-deps bestpkg
在這里我使用 --index-url 參數是為了指定從 test.pypi.org 中安裝,而不是正式包索引庫中查找要安裝的包。還有使用了 --no-deps 參數是因為本例中沒有使用到其它的依賴庫。
在終端會看到以下類似信息,說明安裝成功
1
2
3
4
5
|
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 |
進入交互界面
1
2
3
4
|
(pipenvdemo) ? pipenvdemo python >>> import bestpkg >>> bestpkg.info 'packaging demo' |
info 變量就是在 __init__.py 文件中定義的變量。自此我們的包發布、安裝使用流程就走完了。
要在正式的 Python 索引庫中發布,只需要到pypi.org/注冊賬號,并上傳就可以了。
0x08 總結一下
通過一個簡單的例子展示 Python 通過 setuptools 工具進行打包,然后上傳到 test.pypi.org 的流程。如果要上傳到正式的 pypi.org 上,只需要注冊一個正式的賬號。一旦發布成功就可以使用 pip install [your-package]
的命令進行安裝。
總結
以上所述是小編給大家介紹的Python程序包的構建和發布過程示例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:https://juejin.im/post/5cfbb40151882574ce013a57