zoukankan      html  css  js  c++  java
  • 上传自己的pip模块

    对于模块开发者本质上需要做3件事:

    • 编写模块
    • 将模块进行打包
    • 上传到PyPI(需要先注册PyPI账号)
      • 注册PyPI账号
      • 安装上传工具
      • 基于工具进行上传

    对于模块的使用者来说,只需要做2件事:

    • 通过pip install 模块去安装模块
    • 调用模块

    假设,现在我们要做一个名称叫 zyf_timer 的模块,跟着下面的步骤一步一步的操作。

    第一步 项目文件夹

    根据要求创建如下文件,并填写内容

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

    1.1 License

    LICENSE文件就是咱们模块的许可证,给模块使用者看,说白了就是使用者是否可以免费用于商业用途等。一般开源的软件会选择相对宽泛许可证 MIT,即:作者保留版权,无其他任何限制。

    Copyright (c) 2021 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.

    更多其他许可证参见:https://choosealicense.com/

    1.2 READNE.md

    readme就是对于当前模块的描述信息,一般用于markdown格式编写,如:

    ### 计时器-zyf_timer
    
    #### 安装
    
    > pip install zyf_timer
    
    #### 使用
    
    ```python
    from zyf_timer import timeit
    
    @timeit
    def main():
        time.sleep(1)
        ...
    ```
    
    设置保留位数,默认保留两位小数
    
    ```python
    from zyf_timer import timeit_with_digit
    
    @timeit_with_digit(digit=4)
    def main():
        time.sleep(1)
        ...
    ```

    此文件也可以写模块的简单使用手册,如果手册太复杂建议再创建一个doc文件夹存放使用手册。

    1.3 demos目录

    demos一般会写一些该模块使用的示例,用于使用者快速可以将模块应用到生成中。

    1.4 setup.py

    setup.py文件其实是一个配置文件,用于给setuptools提供一些模块相关的信息,如:模块名称、模块版本、适用的python版本、作者、github地址等。

    # -*- coding: utf-8 -*-
    
    """
    Author     : ZhangYafei
    Description: timer
    """
    
    import setuptools
    
    
    with open("README.md", "r", encoding='utf-8') as fh:
        long_description = fh.read()
    
    
    setuptools.setup(
        name='zyf_timer',  # 模块名称
        version="1.0",  # 当前版本
        author="zhangyafei",  # 作者
        author_email="zhangyafeii@foxmail.com",  # 作者邮箱
        description="函数计时器",  # 模块简介
        long_description=long_description,  # 模块详细介绍
        long_description_content_type="text/markdown",  # 模块详细介绍格式
        # url="https://github.com/zhangyafeii/timer",  # 模块github地址
        packages=setuptools.find_packages(),  # 自动找到项目中导入的模块
        # 模块相关的元数据
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
        # 依赖模块
        # install_requires=[],
        python_requires='>=3.6',
    )

    注意:setuptools是一个包管理工具,可用于打包和安装模块。

    1.5 zyf_timer目录

    插件内容,可以将相关代码在__init__.py编写,

    代码内容

    最后的文件夹如下:

    zyf_timer
    ├── LICENSE
    ├── README.md
    ├── demos
    ├── zyf_timer
    │ ├── __init__.py
    └── setup.py
    

    第二步 代码打包&上传

    上述步骤中将文件夹和代码编写好之后,就需要对代码进行打包处理。

    2.1 安装打包工具(已有无需重复安装)

    打包代码需先安装setuptools和wheel两个工具,可以单独安装,也可以安装pip,从而自动安装这两个工具。

    注意:已安装用户,如要更新setuptools和wheel两个工具,可通过如下命令:

    python -m pip install --upgrade setuptools wheel

    2.2 打包代码

    python setup.py sdist bdist_wheel
    
    zyf_timer
    │  LICENSE
    │  README.md
    │  setup.py
    │
    ├─build
    │  ├─bdist.win-amd64
    │  └─lib
    │      └─zyf_timer
    │              __init__.py
    │
    ├─demos
    ├─dist
    │      zyf_timer-2.0-py3-none-any.whl
    │      zyf_timer-2.0.tar.gz
    │
    ├─zyf_timer
    │      timer.py
    │      __init__.py
    │
    └─zyf_timer.egg-info
            dependency_links.txt
            PKG-INFO
            SOURCES.txt
            top_level.txt
    

    2.3 发布模块(上传)

    文件打包完毕后,需要将打包之后的文件上传到PyPI,如果想要上传是需要先去 https://pypi.org/ 注册一个账号。

      • 安装用于发布模块的工具:twine 【已安装无需重复安装】

    python -m pip install --upgrade twine
    或
    pip install --upgrade twine
    # 提示:python -m 的作用是 run library module as a script (terminates option list)
      • 发布(上传)

    python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
    或
    twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
      • 上传时,提示需要输入PyPI的用户名和密码.

    第三步 安装使用

    3.1 安装

    pip install zyf_timer
    

    如果提示找不到对应的版本,可能是因为你使用的是国内镜像,刚发布的包还未同步,这时可以指明使用官方源下载。

    pip install zyf_timer -i https://pypi.python.org/simple

    3.2 应用

    import time
    from zyf_timer.timer import timeit
    
    
    @timeit
    def main():
        time.sleep(1)
    
    
    if __name__ == '__main__':
        main()
    

      

    作者:张亚飞
    出处:https://www.cnblogs.com/zhangyafei
    gitee:https://gitee.com/zhangyafeii
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    MySql(二)索引的设计与使用
    Perl寻找去除数组中重复元素
    简简单单讲sort--perl
    Perl语言入门笔记 第四章 子程序
    Perl语言入门笔记 第三章 列表和数组
    Perl用CPAN安装模块时错误
    Perl PPM安装模块
    Active Perl的PPM的repository添加
    Perl的CPAN和CPANPLUS安装模块介绍
    CPANPLUS 的使用
  • 原文地址:https://www.cnblogs.com/zhangyafei/p/14391536.html
Copyright © 2011-2022 走看看