• python打包和发布package


    打包

    偶尔有一些复用性很高,复杂度也很高的函数要反复调用,可以自行打包,安装

    打包结构如下
    5.png

    iso_timer为例

    mkdir common
    vim __init__.py
    cd common 
    vim __init__.py
    vim format.py
    
    • 1
    • 2
    • 3
    • 4
    • 5
    # init.py
    from .common import *
    
    
    • 1
    • 2
    • 3
    # /common/init.py
    from .format import *
    
    
    • 1
    • 2
    • 3
    # format.py
    from rich import print
    from datetime import datetime
    
    
    __all__ = ["hello_printf", "iso_now"]
    
    def hello_printf():
        print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())
    
    def iso_now():
        current_time = datetime.now()
        print(current_time.isoformat())
        return current_time.isoformat()
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    # pyproject.toml
    [build-system]
    requires = ["setuptools", "wheel"]
    
    • 1
    • 2
    • 3
    # setup.cfg
    [metadata]
    name = iso_timer
    version = 1.0.0
    description = global use
    author = starlight
    author_email = mylifcc@gmail.com
    
    [options]
    package_dir=
        =src
    packages = find:
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    # setup.py
    from setuptools import setup, find_packages
    
    setup(
        name="iso_timer",
        version="0.1",
        packages=find_packages(where="src"),
        package_dir={"": "src"},
        install_requires=[
            "rich >= 13.6.0",
            # 你的依赖包列表,例如:
            # "matplotlib >= 2.2.0"
        ],
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    然后进行打包
    在包的根目录,我这里是/mylib
    生成包:python setup.py sdist
    安装包: pip install .
    安装后即可使用,注意,这里的文件夹名要和setupname一样

    # test_iso.py
    from iso_timer import *
    
    
    hello_printf()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    发布

    注册

    pypi注册用户 -> 验证邮箱 -> 开通2步验证 -> 创建api_token -> 保存token到本地

    notepad %USERPROFILE%\.pypirc  # for win system
    
    vim $HOME/.pypirc  # for other system
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    用户名固定为__token__
    设置好后进行上传

    pip install twine
    twine upload dist/*
    
    • 1
    • 2

    示例代码在我的 github,如果有问题可以留言。

  • 相关阅读:
    redis缓存击穿 穿透
    电视电话会议和视频会议的区别
    从MVC到DDD,该如何下手重构?
    Redis在Windows和Linux下的安装方法(超级详细)
    设计模式总结
    一文详解Servlet 看这篇就够了
    YOLO目标检测——交通标志数据集+已标注voc和yolo格式标签下载分享
    [附源码]java毕业设计氧气罐管理系统
    推荐100首好听英文歌
    自动化测试的使用场景
  • 原文地址:https://blog.csdn.net/majiayu000/article/details/133983263