制作python模块

在这里插入图片描述

使用pip安装模块的本质:使用者从PYPI去下载开发者网PYPI上产的模块。

PYPI可以理解为一个包(模块)管理平台,模块开发者将自己开发的包上传至PYPI,使用者通过pip intall 去下载。

对于模块开发者来说,需要做三件事:
● 编写模块
● 将模块进行打包
● 上传到PYPI(需要先注册PYPI账号)
○ 注册账号
○ 安装上传工具
○ 基于工具进行上传

对于模块的使用者来说,只需两件事:
● 通过pip install 模块去安装模块
● 调用模块

1. 编写模块(以hpcyhr为例)

结构如下:

  1. hpcyhr
  2. ├── LICENSE # 声明,给模块使用者看,说白了就是使用者是否可以免费用于商业用途等。
  3. ├── README.md # 模块介绍
  4. ├── demos # 使用案例
  5. ├── hpcyhr # 模块代码目录
  6. │└── __ init __.py
  7. └── setup.py # 给setuptools提供信息的脚本(名称、版本等)

1.1 LICENSE

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.

1.2 README

对模块的描述,一般用markdown来书写

hpcyhr is a fantastic tool

1.3 demos目录

写一些模块使用的实例

1.4 setup.py

setyp.py文件其实是一个配置文件,用于给setuptools提供一些模块的相关信息:如模块名称、模块版本、使用的python版本、作者、github地址等
ps:setuptools是一个包管理工具,可用于打包和安装模块。

import setuptools
with open("README.md", "r") as fh:
    long_description = fh.read()
setuptools.setup(
    name="hpcyhr",  # 模块名称
    version="1.0",  # 当前版本
    author="yanghongru",  # 作者
    author_email="hpcyhr@163.com",  # 作者邮箱
    description="一个没有啥用的包",  # 模块简介
    long_description=long_description,  # 模块详细介绍
    long_description_content_type="text/markdown",  # 模块详细介绍格式
    packages=setuptools.find_packages(),  # 自动找到项目中导入的模块
    # 模块相关的元数据
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    # 依赖模块
    install_requires=[
        'requests',
    ],
    python_requires='>=3',
)

1.5 hpcyhr目录

模块相关功能代码存放于此目录下。比如我写一个yhr.py文件
def func():
print(“重铸误差检测荣光,我辈义不容辞!”)

2. 代码打包&上传

2.1 安装打包工具

工具为setuptools和wheel。如果没有,使用pip安装

python -m pip install setuptools
python -m pip install wheel

如果有,可以更新:
python -m pip install --upgrade setuptools wheel
在这里插入图片描述

2.2 代码打包

进入hpcyhr所在的目录,执行如下命令

python setup.py sdist bdist_wheel

在这里插入图片描述
在这里插入图片描述

2.3 发布模块

去 https://pypi.org/注册一个账号
安装发布模块的工具""

python -m pip install --upgrade twine
或
pip install --upgrade twine

在这里插入图片描述

上传:


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

在这里插入图片描述

需要输入账号密码

报错,需要验证邮箱(注意,注册完之后要去邮箱验证)
在这里插入图片描述

验证邮箱之后,上传成功
在这里插入图片描述

3.安装使用

pip 下载本地
在这里插入图片描述
使用
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值