2024最新:如何发布一个python包

104 阅读3分钟

1.填写license(参考:choosealicense.com/licenses/ap…)

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.

2.setup文件和MANIFEST.in (非pyproject.toml项目需要)

setup.py

import setuptools

with open("README.md","r", encoding="utf-8") as fh:
    long_description = fh.read()

# pip install laorm 公共依赖
install_requires = [
    'aiomysql',
]
# pip install laorm[fastapi] 则只会依赖fastapi相关依赖
extras_require = {
    'fastapi': ['fastapi'],
    'flask': ['flask'],
}
setuptools.setup(
    # 包的分发名称,使用字母、数字、_、-
    name="laorm",
     # 版本号, 版本号规范:https://www.python.org/dev/peps/pep-0440/
    version="1.0.0",
    # 作者名
    author="larry",
     # 作者邮箱
    author_email="176286171@qq.com",  
    # 包的简介描述
    description="A python async orm tool",
    # 包的详细介绍(一般通过加载README.md)
    long_description=long_description, 
    # 和上条命令配合使用,声明加载的是markdown文件
    long_description_content_type="text/markdown",
    # 项目开源地址
    url="https://github.com/mryzhou/laorm",
     # 如果项目由多个文件组成,我们可以使用find_packages()自动发现所有包和子包,而不是手动列出每个包,在这种情况下,包列表将是example_pkg
    packages=setuptools.find_packages(),
    # 关于包的其他元数据(metadata)
    classifiers=[
        # 该软件包兼容性:Python3.10
        "Programming Language :: Python :: 3.10",
        # 许可证开源信息
        "License :: OSI Approved :: Apache Software License",
        # 与操作系统无关
        "Operating System :: OS Independent",
    ],
    install_requires=install_requires,
    extras_require=extras_require,
)

MANIFEST.in

recursive 包含得文件夹

exclude 排除得文件夹

recursive-include core
exclude test/*

3.打包

3.1 传统python项目(非pyproject.toml项目)

先安装打包工具

pip install --upgrade pip setuptools wheel

执行打包命令

python setup.py sdist

3.2 针对pyproject.toml打包

poetry build --format=sdist

4.发布

准备工作

创建API Token

  • 访问 pypi.org/manage/acco… 并登录你的账户。
  • 点击“Create a new token”按钮,为你的项目创建一个新的API Token。
  • 给Token一个描述,并选择适当的权限(如“upload packages”)。

=>) tips:2fa认证可以通过浏览器插件Authenticator Extension轻松解决。

方法一:

Unix/Linux系统通常是~/.pypirc,对于Windows可能是 %USERPROFILE%/.pypirc)创建或编辑一个名为 .pypirc 的文件,内容如下

[distutils]
index-servers = pypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: __token__
password: <your-api-token>

请将 your-api-token 替换为你的实际PyPI API Token。

方法二:

直接导出到系统环境变量也是可以的

export TWINE_USERNAME=__token__
export TWINE_PASSWORD=<your-api-token>

然后,你可以使用 twine 命令上传Python包

pip install twine

python -m twine upload dist/*

twine 会自动从 .pypirc 文件中读取配置信息进行身份验证和上传

遇到上传失败修复删除egg-info文件夹后重新打包上传即可

5. 验证

pypi官网可以马上看到,镜像代理有些慢,要等待一会。最后安装使用