有過必定的 Python 經驗的開發者都知道,當引入第三方包時,咱們經常會使用 pip install 命令來下載並導入包。python
那麼,如何寫一個本身的包,上傳到 PyPI 呢,其餘開發者也能夠經過 pip install 命令下載並導入?git
本文提供了最簡單的示例。github
建立一個項目目錄,其目錄結構以下:markdown
/packaging_tutorial /example_pkg __init__.py
其中,packaging_tutorial 是一個文件目錄,example_pkg 是一個你但願上傳的 Python 包。app
注:本人使用的是 virtualenv + virtualenvwrapper 構建的 Python 虛擬環境,所以 python 和 pip 命令(而非 python3 和 pip3)直接對應的是我所指定的虛擬環境(Python 3.6.7)。測試
再向 packaging_tutorial 中建立一些文件。其目錄結構以下:this
/packaging_tutorial /example_pkg __init__.py setup.py LICENSE README.md
在 README.md 能夠輸入一些介紹項目的文檔。url
# 測試 這只是一個測試。 - 測試 1 - 測試 2 - 測試 3
setup.py 是 setuptools 的構建腳本,它提供了包的各類信息。spa
在 setup.py 中輸入如下代碼:rest
1 import setuptools 2 3 with open("README.md", "r") as fh: 4 long_description = fh.read() 5 6 setuptools.setup( 7 name="example-pkg-your-username", 8 version="0.0.1", 9 author="Example Author", 10 author_email="author@example.com", 11 description="A small example package", 12 long_description=long_description, 13 long_description_content_type="text/markdown", 14 url="https://github.com/pypa/sampleproject", 15 packages=setuptools.find_packages(), 16 classifiers=[ 17 "Programming Language :: Python :: 3", 18 "License :: OSI Approved :: MIT License", 19 "Operating System :: OS Independent", 20 ], 21 )
各個配置的字段的含義應該是不言而喻的,若是想了解更多,參見官網解釋。
LICENSE 是項目所遵循的許可證,以 MIT 爲例:
Copyright (c) 2018 The Python Packaging Authority Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
通常來講,pip 默認應該都安裝了 setuptools 和 wheel。若是沒有安裝,則安裝之:
pip install setuptools wheel -i https://pypi.douban.com/simple
若是安裝須要更新,則更新之:
pip install --upgrade setuptools wheel -i https://pypi.douban.com/simple
安裝好最新版本後,在 setup.py 所在目錄下輸入:
python setup.py sdist bdist_wheel
這個命令會在生成一個 dist 目錄,裏面有兩個文件:
dist/ example_pkg_your_username-0.0.1-py3-none-any.whl example_pkg_your_username-0.0.1.tar.gz
tar.gz 是源文件存檔,whl 是構建的發佈版本。
安裝 twine:
pip install twine -i https://pypi.douban.com/simple
安裝好以後,執行 twine 命令(這裏,須要注意你已經註冊了 PyPI 的帳號):
twine upload dist/*
Enter your username: heyulong Enter your password: Uploading distributions to https://upload.pypi.org/legacy/ Uploading example_pkg_heyulong-0.0.1-py3-none-any.whl 100%|██████████████████████████████████████| 5.49k/5.49k [00:01<00:00, 4.66kB/s] Uploading example-pkg-heyulong-0.0.1.tar.gz 100%|██████████████████████████████████████| 4.23k/4.23k [00:01<00:00, 2.21kB/s]
執行完以後,便可在 PyPI 官網上看到本身上傳的項目了。
安裝你上傳的 PyPI 項目,好比個人:
pip install example-pkg-heyulong
這裏簡單介紹了上傳 PyPI 項目的過程。更多細節請關注 PyPI 官網。