【Python】封装、打包和公开库

封装

创建目录结构,我们接下来的命令都在 ifree/__init__.py 目录进行

/ifree
  /setup.py 
  /ifree
    /__init__.py
  1. 编辑 ifree/ifree/__init__.py,导入该库中的各个模块
import ifree.processing 
import ifree.dicom
  1. 编辑 ifree/setup.py

setup.pysetuptools 的构建脚本,提供PYPI的库的信息(库名、版本信息、描述、环境需求等)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""The setup script."""

from setuptools import setup, find_packages


with open('README.md', 'r') as readme_file:
    readme = readme_file.read()

# 需要的库
requirements = [
    "sklearn",
    "tqdm",
]

# 详细信息
setup(
    name='ifree',
    version='0.1.0',
    description="i love freedom, free my hand.",
    long_description=readme,
    long_description_content_type='text/markdown',
    author="Lin Zhenyu",
    author_email='linzhenyu1996@gmail.com',
    url='https://github.com/linzhenyuyuchen/ifree',
    packages=find_packages(include=['ifree']),
    include_package_data=True,
    install_requires=requirements,
    license="BSD license",
    zip_safe=False,
    keywords='ifree',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.6',
    ],
    python_requires='>=3.6'
)

  • name:库名version:版本号
  • author:作者
  • author_email:作者邮箱
  • description:简述
  • long_description:详细描述
  • long_description_content_type:README.md中描述的语法(一般为markdown)
  • url:库/项目主页,一般我们把项目托管在GitHub,放该项目的GitHub地址即可packages:使用setuptools.find_packages()即可,这个是方便以后我们给库拓展新功能的(详情请看官方文档)
  • classifiers:指定该库依赖的Python版本、license、操作系统之类的

打包

创建 packaging_tutorial/LICENSE , 用于商用或者其它免责声明。

可选择的LICENSE

MIT License

Copyright (c) [year] [fullname]

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.

安装最新的setuptools, wheeltwine

python3 -m pip install --user --upgrade setuptools wheel twine
cd ./ifree
python3 setup.py sdist bdist_wheel

目录下生成以下文件:

dist/
  example_pkg_your_username-0.0.1-py3-none-any.whl
  example_pkg_your_username-0.0.1.tar.gz

使用 twine 将打包好的库/项目上传到PYPI 测试服务器

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

测试一下这个库能不能用

python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-pkg-your-username

公开

注册链接

使用 twine 将打包好的库/项目上传到PYPI 正式服务器

twine upload dist/*

公开后,安装方法:

pip install ifree==0.1.0

官方教程

更新版本

需要在setup.py中更新版本号

图标生成工具

link

posted @ 2021-05-07 21:51  林震宇  阅读(460)  评论(0编辑  收藏  举报