python打包和发布package

打包

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

打包结构如下
5.png

iso_timer为例

mkdir common
vim __init__.py
cd common 
vim __init__.py
vim format.py
# init.py
from .common import *

# /common/init.py
from .format import *

# 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()
    
# pyproject.toml
[build-system]
requires = ["setuptools", "wheel"]
# 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:

# 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"
    ],
)

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

# test_iso.py
from iso_timer import *


hello_printf()

发布

注册

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

notepad %USERPROFILE%\.pypirc  # for win system

vim $HOME/.pypirc  # for other system

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

pip install twine
twine upload dist/*

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值