如何將自己的python庫打包成wheel文件並上傳到pypi

新建項目

如下圖,比如sigma目錄是我要上傳的項目,在six-sigma目錄下新建三個文件,分別是LICENSE也就是開源協議,README.md文件,用於介紹自己的項目和setup.py這個配置文件,此文件配置關於項目和作者的一些信息,接下來我們一一介紹。

在這裡插入圖片描述

LICENSE文件

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.

README.md文件

# 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.

setup.py文件

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="sigma_t",  # 項目名稱
    version="0.0.1",  # 項目版本信息
    author="AlanRick",  # 作者  寫你的真實姓名即可
    author_email="[email protected]",  # 作者郵箱
    description="six sigma project",  # 項目簡介
    long_description=long_description,  # 項目詳細的介紹  這裡直接讀取README.md文件
    long_description_content_type="text/markdown",  # 項目詳細介紹的文件類型
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir={"": "src"},  # 自己的包所在目錄
    packages=setuptools.find_packages(where="src"),  # 所有模塊所在目錄
    python_requires=">=3.6",  # python所需要的版本
)

安裝所需插件

確保您已經在pypi.org上註冊瞭賬號,然後執行以下命令

pip install wheel
pip install twine

打包文件為*.whl

首先在終端cd到setup.py文件所在目錄下,並在終端執行以下命令進行打包

python setup.py bdist_wheel

打包完成生成如下文件

在這裡插入圖片描述

上傳包至pypi

python -m twine upload  dist/sigma-0.0.1-py3-none-any.whl

如下在終端提示您輸入用戶名和密碼然後進行上傳

在這裡插入圖片描述

如下上傳成功

在這裡插入圖片描述

在pypi項目管理頁面可以看到剛上傳好的文件

在這裡插入圖片描述

點開項目可以看到我們的配置文件顯示在前端瞭

在這裡插入圖片描述
在這裡插入圖片描述

結語

也可以參考pypi官網的方法進行上傳。

到此這篇關於如何將自己的python庫打包成wheel文件並上傳到pypi的文章就介紹到這瞭,更多相關python庫打包成wheel並上傳到pypi內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: