PYPI开发和上传包

Python是一个与时俱进的编程语言,人人都可以向社区共享代码,pypi是第三方模块的集中仓库。本文将给大家介绍如何通过setuptools和wheel工具创建模块,并利用twine工具发布到pypi社区的方法。

1.编写自己的模块

#python3.7
#模块名称showlist.py
#模块包含方法print_list(),打印多维列表。
def print_list(lst,indent=0):
    '''
        打印多维列表,初始缩进indent = 0,以后每级缩进4个'----'。
    '''
    for item in lst:
        if isinstance(item,list):
            print_list(item,indent + 1)
        else:
            print('----'*indent + str(item))

创建模块的准备文件

1.创建目录modules
2.进入目录modules,创建文件READMEmd、LICENSE、setup.py。创建子目录modules_zn_1 在子目录中存放showlist.py和init.py。

setup.py是用来构建模块的脚本
import setuptools

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

setuptools.setup(
    name="modules_zx_1",
    version="0.0.1",
    author="zhangning",
    author_email="[email protected]",
    description="A small  package to print nested lists",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

尤其注意name对应的包的名字(子文件夹的名字),不能与已有包重名。另外注意调用的函数find_packages()可以调用当前目录下所有的包。

README.md文件内容,是对包的内容和功能的介绍,最好包含例子
#example
from modules_zn_1 import showlist
district = ['USA','CHINA',['Hebei ','Shandong',Beijing',['Fengtai','Haidian']]]
showlist.print_list(district)
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.
init.py文件仅需要提供包的名称即可
name = 'modules_zn_1'

2.构建和发布包

python -m pip install --user --upgrade wheel setuptools

进入modules目录,构建包文件,结果生成dist文件,内含两个构建完成的包文件.tar.gz和.whl文件

python setup.py sdist bdist_wheel 

1.安装twine包,用于上传包文件

python -m pip install --user --upgrade twine

上传到社区PYPI

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

输入用户名和密码

Enter your username: NINGZHANG_PIPY
Enter your password: 

在你的PYPI网页中可以查看上传的包文件了

可以在PYPI安装自己的模块了

python -m pip install --index-url https://upload.pypi.org/legacy/ showlist

测试导入的模块的功能

from modules_zn_1 import showlist
district = ['USA','CHINA',['Hebei ','Shandong',Beijing',['Fengtai','Haidian']]]
showlist.print_list(district)

结束语,以后整理好处理某一类型的脚本和函数,可以整理成自己的包文件。

本文章参考于:作者:辣椒爸

你可能感兴趣的:(PYPI开发和上传包)